yuanheng's profileοo怅惘ヤo ACM-ICPC HomePagePhotosBlogListsMore Tools Help

οo怅惘ヤo ACM-ICPC HomePage

- - 宅男的我 继续被水题虐去 -v- | 默默地做事,朝着自己的目标一步步前进!
February 05

祝贺SJTU夺冠

2010年ACM-ICPC World Finals完美谢幕,SJTU勇夺冠军,膜拜!
同时,大会也宣布了2011年Finals将在埃及的开罗大学举行!

吃了顿羊肉-v-饱饱

今晚吃了顿羊肉,味道特爽^_^
还有9个多小时就final了
January 31

不再公开我的解题报告了

由于某些原因,不再在blog上公开我的OJ解题报告了 T_T

ACM-ICPC全球总决赛现场直播 http://live6.hrbeu.edu.cn
由于没有钱,承担不起自费去观摩Final唯有通过现场直播了...

迟点有可能过去澳门玩儿。。。

睡觉去,明个儿早起继续奋斗!

January 25

- - 感冒了 T_T

今天早上一觉醒来按照以往的惯例到冲凉房刷牙、冲一个热水澡
因为洗完澡之后的舒适度比较好,有利于接下来的训练
没有想到的是,洗完澡回到自己的房间之后突然觉得有点晕晕的感觉
对着电脑屏幕总觉得眼前出现很多星星,而且有种想呕吐的预兆
就跑到洗手间,果然呕了出来。。。
接下来头特别疼、接二连三的跑到洗手间去呕吐(呕到连胃液都呕了出来=_=!)
这下子肯定是冲热水澡不小心着凉了T_T
马上去了趟伯父家吊了一瓶点滴、回来后也吃了药
现在感冒应该没了~~~
January 24

POJ 1915 解题报告

Knight Moves
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 9237
Accepted: 4189

Description

Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

Source

TUD Programming Contest 2001, Darmstadt, Germany

【算法介绍】:BFS(Breadth – First Search)

【题目描述】:第一行一个整数n,代表有n组测试数据。每组测试数据有3,第一行为棋

盘的规模k,也就是(0...k-1)*(0...k-1)的棋盘,第二行是骑士的起始

位置(x1,y1),3行市骑士的终点位置(x2,y2)。问:对于每组测试数据,

求出从起始位置到终点位置所跳的最少步数。

【题目分析】:广度优先搜索去搞就可以了。


#include <cstdio>
#include <cstring>

bool mark[300][300];
int sum[300][300];
//sum[i][j]计算起始位置到[i][j]的最少步数
int queue[90000][2];
int x[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int y[8] = {-2, -1, 1, 2, 2, 1, -1, -2};

void bfs(int p, int q, int l, int r, int k)
{
int i;
int tx, ty, px, py;
int head = 0, tail = 1;
queue[0][0] = p;
queue[0][1] = q;
mark[p][q] = true;
sum[p][q] = 0;
if (p == l && q == r) return;
while (head < tail)
{
tx = queue[head][0];
ty = queue[head][1];
head++;
for (i = 0; i < 8; i++)
{
px = tx + x[i];
py = ty + y[i];
if (px >= 0 && px < k && py >= 0 && py < k && (!mark[px][py]))
{
queue[tail][0] = px;
queue[tail][1] = py;
tail++;
mark[px][py] = true;
sum[px][py] = sum[tx][ty] + 1;
}
}
if (px == l && py == r) break;
}
}

int main()
{
int n, i, k, x1, y1, x2, y2;
scanf("%d",&n);
for (i = 0; i < n; i++)
{
scanf("%d",&k);
scanf("%d %d",&x1, &y1);
scanf("%d %d",&x2, &y2);
memset(mark,false,sizeof(mark));
memset(sum,0,sizeof(sum));
bfs(x1, y1, x2, y2, k);
printf("%d\n",sum[x2][y2]);
}
return 0;
}
 

yuanheng

Occupation
Location
Interests
The Websites Of Programming Contest
The Blogs Of My Good Friends
感谢访问!
Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.