zoj3761(BFS)
来源:互联网 发布:hanoi塔c语言递归详解 编辑:程序博客网 时间:2024/05/12 07:36
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5206
Edward think a game of billiards is too long and boring. So he invented a new game called Easy billiards.
Easy billiards has N balls on a brimless rectangular table in the beginning, and your goal is try to make the number of balls on the table as least as possible by several hit under the following rules:
1: The direction you hit the balls should parallel to the tables border.
2: If ball A crashed into ball B, ball B will moves in the same direction of ball A before the crashing, and ball A will stop in the place of ball B before the crashing.
3: If ball C is moving and there are no balls in front of ball C, it will runs out of the tables border, that means ball C is out of the table.
4: You can choose arbitrary ball on the table to hit, but on a hit, you can't let the ball you choose to hit runs out of the tables border. In another word, a ball could runs out of the table if and only if it was crashed by another ball in a hitting.
Now, Edward wants to know the least number of balls remained on the table after several hits, and how.
Input
There are multiple test cases. For each test cases, in the first line, there is an integer N, which means the number of the balls on the table. There are following N lines, each line contains two integers Xi and Yi, which means the coordinate of ball I. (0<=N<=2000, 0<=Xi, Yi<=10^8)
Output
For each test cases, you should output the least number of balls on the first line. And you should output several lines to show the order of hits following the first line, each line should contains the coordinate of the ball you choose to hit and the direction you hit. (LEFT,RIGHT,UP,DOWN).
Sample Input
40 02 04 02 291 12 13 11 22 23 21 32 33 3
Sample output
1(2, 2) DOWN(4, 0) LEFT(2, 0) LEFT1(1, 3) DOWN(1, 2) DOWN(2, 3) DOWN(2, 2) DOWN(3, 3) DOWN(3, 2) DOWN(3, 1) LEFT(2, 1) LEFT
思路:用广搜,所以搜一个球,将可以连接到的球全部撞向它,然后将它可以连接到的球能够连接的球再撞向它可以连接到的球,以此类推。A的时候有些某明奇妙,一开始用两个FOR循环现将最少剩下的球的数目输出来,并将可以相连的球都归为一类,然后搜索路径,但不知为什么一直WA。实在无奈,就简化下代码,在搜索时顺便求最小剩下的球的数目,没想到这样就A了。
代码:
AC代码(简化代码):
#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define LL __int64#define Max 2222struct point{ int x,y,bj;} pp[2010];int n;bool vi[2010];vector<point> ans; //用VECTOR记录路径,最后倒着输出bool kmp(point a,point b){ return a.bj<b.bj;}void bfs(int here){ queue<int> qi; qi.push(here); vi[here]=0; while(!qi.empty()){ here=qi.front(); qi.pop(); int fap[10]= {0}; for(int i=1; i<=n; i++){ //这里是判断与其相连的最近的球 if(pp[i].x==pp[here].x&&pp[i].y<pp[here].y){ if(!fap[1]) fap[1]=i; else if(pp[here].y-pp[i].y<pp[here].y-pp[fap[1]].y) fap[1]=i; } if(pp[i].x==pp[here].x&&pp[i].y>pp[here].y){ if(!fap[2]) fap[2]=i; else if(pp[i].y-pp[here].y<pp[fap[2]].y-pp[here].y) fap[2]=i; } if(pp[i].x<pp[here].x&&pp[i].y==pp[here].y){ if(!fap[3]) fap[3]=i; else if(pp[here].x-pp[i].x<pp[here].x-pp[fap[3]].x) fap[3]=i; } if(pp[i].x>pp[here].x&&pp[i].y==pp[here].y){ if(!fap[4]) fap[4]=i; else if(pp[i].x-pp[here].x<pp[fap[4]].x-pp[here].x) fap[4]=i; } } for(int i=1; i<5; i++){ if(fap[i]&&vi[fap[i]]){ vi[fap[i]]=0; qi.push(fap[i]); point s; s.x=pp[fap[i]].x; s.y=pp[fap[i]].y; s.bj=i; ans.push_back(s); } } }}int main(){ while(scanf("%d",&n)>0){ for(int i=1; i<=n; i++) scanf("%d%d",&pp[i].x,&pp[i].y); int len=0; memset(vi,1,sizeof(vi)); ans.clear(); for(int i=1; i<=n; i++) if(vi[i]){ bfs(i); len++; } printf("%d\n",len); for(int i=ans.size()-1; i>=0; i--){ printf("(%d, %d) ",ans[i].x,ans[i].y); if(ans[i].bj==1) puts("UP"); else if(ans[i].bj==2) puts("DOWN"); else if(ans[i].bj==3) puts("RIGHT"); else puts("LEFT"); } } return 0;}WA代码(原先的复杂代码):
#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define LL __int64#define Max 2222struct point{ int x,y,bj;} pp[2010];int n;bool vi[2010];bool kmp(point a,point b){ return a.bj<b.bj;}void bfs(int here){ queue<int> qi; vector<point> ans; qi.push(here); vi[here]=0; while(!qi.empty()) { here=qi.front(); qi.pop(); int fap[10]= {0}; for(int i=1; i<=n; i++) { if(pp[i].bj<pp[here].bj) continue; if(pp[i].bj>pp[here].bj) break; if(pp[i].x==pp[here].x&&pp[i].y<pp[here].y) { if(!fap[1]) fap[1]=i; else if(pp[here].y-pp[i].y<pp[here].y-pp[fap[1]].y) fap[1]=i; } if(pp[i].x==pp[here].x&&pp[i].y>pp[here].y) { if(!fap[2]) fap[2]=i; else if(pp[i].y-pp[here].y<pp[fap[2]].y-pp[here].y) fap[2]=i; } if(pp[i].x<pp[here].x&&pp[i].y==pp[here].y) { if(!fap[3]) fap[3]=i; else if(pp[here].x-pp[i].x<pp[here].x-pp[fap[3]].x) fap[3]=i; } if(pp[i].x>pp[here].x&&pp[i].y==pp[here].y) { if(!fap[4]) fap[4]=i; else if(pp[i].x-pp[here].x<pp[fap[4]].x-pp[here].x) fap[4]=i; } } for(int i=1; i<5; i++) if(fap[i]&&vi[fap[i]]) { vi[fap[i]]=0; qi.push(fap[i]); point s; s.x=pp[fap[i]].x; s.y=pp[fap[i]].y; s.bj=i; ans.push_back(s); } } for(int i=ans.size()-1;i>=0;i--) { printf("(%d, %d) ",ans[i].x,ans[i].y); if(ans[i].bj==1) puts("UP"); else if(ans[i].bj==2) puts("DOWN"); else if(ans[i].bj==3) puts("RIGHT"); else puts("LEFT"); }}int main(){ pp[0].bj=0; while(scanf("%d",&n)>0) { int len=1; for(int i=1; i<=n; i++) { scanf("%d%d",&pp[i].x,&pp[i].y); pp[i].bj=Max; } for(int i=1; i<=n; i++) { if(pp[i].bj==Max) pp[i].bj=++len; for(int j=1; j<=n; j++) { if(pp[i].x==pp[j].x||pp[i].y==pp[j].y) { pp[i].bj=min(pp[i].bj,pp[j].bj); pp[j].bj=min(pp[i].bj,pp[j].bj); } } } sort(pp+1,pp+n+1,kmp); len=0; for(int i=1,j=Max; i<=n; i++) { if(pp[i].bj==j) pp[i].bj=len; else { j=pp[i].bj; pp[i].bj=++len; } } printf("%d\n",len); memset(vi,1,sizeof(vi)); for(int i=1;i<=n;i++) if(pp[i].bj!=pp[i-1].bj) bfs(i); } return 0;}
- zoj3761(BFS)
- zoj3761 Easy billiards
- hdu1254(bfs+bfs)
- hdu1254(嵌套bfs,两次bfs)
- hdu 1254 推箱子(bfs + bfs)
- hdu1254 推箱子(bfs+bfs)
- BFS:HDU3085-Nightmare Ⅱ(双向BFS)
- HDU 1072(BFS)
- uva 784(bfs)
- HDU 1429(BFS )
- 八数码(BFS)
- USACO / Overfencing(BFS)
- SOJ-3330(bfs)
- Rescue(BFS)
- Knight Moves(bfs)
- (BFS)3414 Pots
- hdu 3345 (BFS)
- 连连看(BFS)
- String方法
- word中的visio图中的中文打印不出来的解决办法
- Thinking in C++读书笔记(二)---对象的创建与使用
- C#所引器
- 割据的逻辑:中国法律服务市场的生态分析
- zoj3761(BFS)
- BITACM 1047 Fibonacci Numbers
- 查找循环数组中元素位置(无重复元素)
- poj 1979
- HDOJ 4118 Holiday's Accommodation
- java.io.NotSerializableException的解决办法
- Android中对Log日志文件的分析
- 题目1204:农夫、羊、菜和狼的故事
- poj2503 Barbelfish