题目链接:题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511
1511: 残缺的棋盘
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 169 Solved: 56
[Submit][Status][Web Board]
Description
Input
输入包含不超过10000 组数据。每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同。
Output
对于每组数据,输出测试点编号和最少步数。
Sample Input
1 1 8 7 5 6
1 1 3 3 2 2
Sample Output
Case 1: 7
Case 2: 3
HINT
Source
湖南省第十届大学生计算机程序设计竞赛
题目大意:在8*8的棋盘内,给你三个点A,B,C,求从A到B且不经过C的最短步骤,可以有周围八个方向。
分析
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int r1,c1,r2,c2,r3,c3;
int vis[11][11];
struct node
{
int x,y,step;
}st;
node t[111];
void bfs(node st)
{
queue<node> q;
node now,next;
now.step = 0;
vis[st.x][st.y] = 1;
q.push(st);
while (!q.empty())
{
now = q.front();
q.pop();
if (now.x==r2 && now.y==c2)
{
printf ("%d\n",now.step);
return ;
}
for (int i=0;i<8; i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
if (!vis[next.x][next.y]&&next.x>0&&next.x<9&&next.y>0&&next.y<9&&(next.x!=r3||next.y!=c3))
{
vis[next.x][next.y] = 1;
next.step = now.step + 1;
q.push(next);
}
}
}
}
int main ()
{
int ii=1;
while (scanf ("%d%d%d%d%d%d",&r1,&c1,&r2,&c2,&r3,&c3)==6)
{
printf ("Case %d: ",ii++);
memset(vis, 0, sizeof(vis));
st.x = r1;
st.y = c1;
bfs(st);
}
return 0;
}