hdu 3500 DFS(限定)
2016-04-05 19:44
253 查看
Fling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 455 Accepted Submission(s): 190
[align=left]Problem Description[/align]
Fling is a kind of puzzle games available on phone.
This game is played on a board with 7 rows and 8 columns. Each puzzle consists of a set of furballs placed on the board. To solved a puzzle, you need to remove the furballs from board until there is no more than one furball on the board. You do this by ´flinging´ furballs into other furballs, to knock them off the board. You can fling any furballs in four directions (up, left, right, down). The flung furball stops at the front grid of another one as soon as knocking it. And the knocked furball continues to rolling in the same direction until the last knocked one goes off the board. For instance, A furball at (0, 0) rolls right to the furball at (0, 5), then it will stop at (0, 4). Moreover, the latter will roll to right. You cannot fling a furball into a neighbouring furball, the one next to in any of four directions. However, it is permitted for a rolling ball knocks into a ball with a neighbour in that direction.
[align=left]Input[/align]
The input contains multiple test cases.
For each case, the 7 lines with 8 characters describe the board. ´X´ represents a empty grid and ´O´ represents a grid with a furball in it. There are no more than 12 furballs in any board.
Each case separated by a blank line.
[align=left]Output[/align]
For each case, print a line formatted as "CASE #NUM:", where NUM is the number of current case.
Then every ´fling´ prints a line. Each line contains two integers X, Y and a character Z. The flung furball is located at grid (X, Y), the top-left grid is (0, 0). And Z represents the direction this furball towards: U (Up), L (Left), R (Right) and D (Down);
Print a blank line between two cases.
You can assume that every puzzle could be solved.
If there are multiple solve sequences, print the smallest one. That is, Two sequences A (A1, A2, A3 ... An) and B (B1, B2, B3 ... Bn). Let k be the smallest number that Ak != Bk.
Define A < B :
(1) X in Ak < X in Bk;
(2) Y in Ak < Y in Bk and X in Ak = X in Bk;
(3) Z in Ak < Z in Bk and (X,Y) in Ak = (X,Y) in Bk;
The order of Z: U < L < R < D.
[align=left]Sample Input[/align]
XXXXXXXX XXOXXXXX XXXXXXXX XXXXXXXX XOXXXXOX XXXXXXXX XXXXXXXX XXXXXXXX XOXOXOOX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
[align=left]Sample Output[/align]
CASE #1:
4 6 L
1 2 D
CASE #2:
1 1 R
1 4 L
1 3 R
给出在一个7*8方块上的很多毛球,允许的操作只有用一个毛球取弹另一个毛球,最终的状态是只剩下一个球
其中:
不能直接把毛球弹出去
假如相邻也是一个毛球,则不能往这个方向弹(可理解为无法蓄力)。
使用dfs()进行搜索,对一个球弹或者不弹进行搜索,记住要使用一个空地图来存储决策前的地图,以便在搜索方向出错是进行回退
#include <iostream> #include <cstring> #include <cstdio> using namespace std; struct node { int x,y,z; }ans[60]; char mat[10][10]; char direc[]={'U','L','R','D'}; int nx[]={-1, 0, 0, 1}; int ny[]={ 0,-1, 1, 0}; void fling(int x,int y,int k) { int flag=0; int nxtx=x+nx[k],nxty=y+ny[k]; if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) {mat[x][y]='X';return;} while(mat[nxtx][nxty]=='X') { nxtx+=nx[k];nxty+=ny[k]; if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) {flag=1;break;} } if(flag) {mat[x][y]='X';return;} mat[x][y]='X'; mat[nxtx-nx[k]][nxty-ny[k]]='O'; fling(nxtx,nxty,k); } bool dfs(int sum,int cnt) { if(sum==1) { return 1; } int nxtx,nxty; char t_mat[10][10]; memcpy(t_mat,mat,sizeof(mat)); for(int i=0;i<7;i++) { for(int j=0;j<8;j++) { if(mat[i][j]!='O') continue; for(int k=0;k<4;k++) { nxtx=i+nx[k];nxty=j+ny[k]; if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) continue; if(mat[nxtx][nxty]=='O') continue; int flag=0; while(mat[nxtx][nxty]=='X') { nxtx+=nx[k];nxty+=ny[k]; if(nxtx<0||nxtx>=7||nxty<0||nxty>=8) { flag=1;break; } } if(flag) continue; mat[i][j]='X'; mat[nxtx-nx[k]][nxty-ny[k]]='O'; fling(nxtx,nxty,k); ans[cnt].x=i; ans[cnt].y=j; ans[cnt].z=k; if(dfs(sum-1,cnt+1)) return 1; memcpy(mat,t_mat,sizeof(t_mat)); } } } return 0; } int main() { int ca=1; while(scanf("%s",mat[0])!=EOF) { int sum=0; for(int i=1;i<7;i++) scanf("%s",mat[i]); for(int i=0;i<7;i++) { for(int j=0;j<8;j++) { if(mat[i][j]=='O') { sum++; } } } memset(ans,-1,sizeof(ans)); dfs(sum,0); if(ca!=1) puts(""); printf("CASE #%d:\n",ca++); for(int i=0;ans[i].x!=-1;i++) { printf("%d %d %c\n",ans[i].x,ans[i].y,direc[ans[i].z]); } } return 0; }
相关文章推荐
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter 12. K-Means Clustering
- RadioGroup标签及游标展示
- UVa 10340, All in All
- 第五周项目2-游戏中的角色类(1)
- SDAU 1004
- 20145229吴姗珊《Java程序设计》2天总结
- java学习第六天
- iOS框架学习
- 启用 mvc webapi 的 session功能可用
- 第五周学习进度情况
- 第三周项目:个人所得税计算
- 第6周项目2带武器的游戏角色
- maven项目报错:找不到编译器
- mysql查看慢查询日志
- frameset跳出框架-自动跳转
- 前端开发知识点总结
- Odoo8.0不能创建客户问题的解决 Document type: res.partner, Operation: read
- c++中 有关自定义string的那些为什么
- SDAU 1001
- java中各种进制的转换