csu1511——残缺的棋盘(bfs)
2017-05-02 16:47
330 查看
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
简单的bfs,第三个点处理成不能走就行了
#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue> #include <cstdio> #include <set> #include <math.h> #include <algorithm> #include <queue> #include <iomanip> #include <ctime> #define INF 0x3f3f3f3f #define MAXN 100005 #define Mod 1000000007 using namespace std; int r1,c1,r2,c2,r3,c3; int vis[10][10]; int dx[]={1,-1,0,0,1,-1,1,-1}; int dy[]={0,0,1,-1,1,1,-1,-1}; struct Node { int x,y,step; }; int bfs(int x,int y) { Node start; start.x=x,start.y=y; start.step=0; vis[x][y]=1; queue<Node> q; q.push(start); while(!q.empty()) { Node tmp=q.front(),tmp1; q.pop(); if(tmp.x==r2&&tmp.y==c2) { return tmp.step; } for(int i=0;i<8;++i) { tmp1=tmp; int xx=tmp1.x+dx[i]; int yy=tmp1.y+dy[i]; if(xx<1||xx>8||yy<1||yy>8||vis[xx][yy]) continue; vis[xx][yy]=1; tmp1.x=xx; tmp1.y=yy; tmp1.step++; q.push(tmp1); } } } int main() { int cnt=1; while(~scanf("%d%d%d%d%d%d",&r1,&c1,&r2,&c2,&r3,&c3)) { memset(vis,0,sizeof(vis)); vis[r3][c3]=1; printf("Case %d: %d\n",cnt++,bfs(r1,c1)); } return 0; }
相关文章推荐
- CSU 1511: 残缺的棋盘(BFS啊 )
- CSU 1511 残缺的棋盘
- CSU 1511——残缺的棋盘
- CSU-1511 残缺的棋盘 解题报告
- ACM--BFS--残缺的棋盘--CUS 1511--第十届湖南省赛
- CSU 残缺的棋盘 (BFS)
- CSUOJ 1511 残缺的棋盘(BFS)
- csuoj 1511: 残缺的棋盘
- CSU 1511 残缺的棋盘
- CSU_1511_残缺的棋盘
- CSU 1511 残缺的棋盘 第十届湖南省赛题
- CSU 1511 残缺的棋盘
- 1511: 残缺的棋盘 coj
- 湖南省第十届大学生计算机程序设计竞赛1511: 残缺的棋盘
- CSU1511-残缺的棋盘-模拟/BFS
- COJ 1511:残缺的棋盘
- CSU1511(BFS)
- ZCMU-1639-残缺的棋盘
- 残缺棋盘 【分治】
- POJ2965 BFS(自己写)+状态压缩 OR 偶数格子的棋盘规律