SDAU课程练习2 1022
2016-04-02 17:14
399 查看
Pusher
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/65536K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 2
Special Judge
Problem Description
PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, and there are piles of blocks on some positions. The goal is to clear the blocks by pushing into them. <br><br>You should choose an empty area as the initial position of the PusherBoy.
Then you can choose which direction (U for up, D for down, L for left and R for right) to push. Once the direction is chosen, the PusherBoy will walk ahead until he met a pile of blocks (Walking outside the grid is invalid). Then he remove one block from the
pile (so if the pile contains only one block, it will become empty), and push the remaining pile of blocks to the next area. (If there have been some blocks in the next area, the two piles will form a new big pile.)<br><br>Please note if the pusher is right
up against the block, he can't remove and push it. That is, there must be a gap between the pusher and the pile. As the following figure, the pusher can go up, but cannot go down. (The cycle indicates the pusher, and the squares indicate the blocks. The nested
squares indicate a pile of two blocks.)<br><center><img src=../../../data/images/2821-1.jpg></center> <br><br>And if a whole pile is pushed outside the grid, it will be considered as cleared.<br>
Input
There are several test cases in each input. The first two lines of each case contain two numbers C and R. (R,C <= 25) Then R lines follow, indicating the grid. '.' stands for an empty area, and a lowercase letter stands for a pile of blocks. ('a' for one block,
'b' for two blocks, 'c' for three, and so on.)<br><br>
Output
Output three lines for each case. The first two lines contains two numbers x and y, indicating the initial position of the PusherBoy. (0 <= x < R, 0 <= y < C). The third line contains a moving sequence contains 'U', 'D', 'L' and 'R'. Any correct answer will
be accepted.
Sample Input
3<br>7<br>...<br>...<br>.b.<br>...<br>...<br>.a.<br>...<br>
Sample Output
4<br>1 UDU
Source
2009 Multi-University Training Contest 1 - Host by TJU
题目大意:
上面有链接,可以去玩一下这个游戏再来看题。。
可以上下左右移动,移动遇到箱子终止,然后把箱子朝着当前方向推出一格,箱子数量减少 1
限制条件,当前位置要和箱子有一个距离才推得动箱子。
要求输出的是起点坐标和操作。
思路:
感觉用到的模拟更多一些, dfs 进行了起点坐标的检索和推动方向的检索,大概是这样吧。
感想:
参考别人博客才写出来的, dfs 感觉遇到了一个卡住的地方。
还有就是 dfs 不只是 回溯,一开始想着回溯来着,但是写不出来,也可能这个题不是“严格的dfs”
我觉得还是和贪心一样, dfs 也只是一个思想,结合其他的在一起用才能发挥出来效果,
AC代码:
#include<cstdio> #include<cstring> #include<cctype> int n,m,num,cnt,a[35][35],dirx[4]={0,0,-1,1},diry[4]={-1,1,0,0}; char mapp[35][35],p[]="LRUD",path[625]; int isend(int x,int y) { if(x<0||y<0||x>=n||y>=m) return 0; if(a[x][y]==0) return 1; return -1; } int dfs(int x,int y,int pos) { int i,xx,yy,tx,ty; for(i=0;i<4;i++) { xx=x+dirx[i]; yy=y+diry[i]; if(isend(xx,yy)!=1) continue; do { xx+=dirx[i]; yy+=diry[i]; }while(isend(xx,yy)==1); if(isend(xx,yy)&&isend(tx=xx+dirx[i],ty=yy+diry[i])&&a[xx][yy]) { int t=a[xx][yy]; path[pos]=p[i]; if(t==1||a[tx][ty]) cnt--; if(cnt==0) { num=pos; return 1; } a[tx][ty]+=t-1; a[xx][yy]=0; if(dfs(xx,yy,pos+1)) return 1; a[xx][yy]=t; a[tx][ty]-=t-1; if(t==1||a[tx][ty]) cnt++; } } return 0; } int ma 4000 in() { // freopen("r.txt","r",stdin); while(~scanf("%d%d",&m,&n)) { int i,j,k,x,y,flag=1,f[35][35]={0}; for(i=num=0;i<n;i++) { scanf("%s",mapp[i]); for(j=0;j<m;j++) if(isalpha(mapp[i][j])) num++,a[i][j]=mapp[i][j]-'a'+1; else a[i][j]=0; } for(i=0;i<n&&flag;i++) for(j=0;j<m&&flag;j++) if(a[i][j]) for(k=0;k<4&&flag;k++) { x=i+dirx[k]*2; y=j+diry[k]*2; if(isend(x,y)>0&&!f[x][y]) { f[x][y]=1; cnt=num; if(dfs(x,y,0)) flag=0; } } printf("%d\n%d\n",x,y); for(i=0;i<=num;i++) printf("%c",path[i]); puts(""); } }
相关文章推荐
- 前端需要了解的
- Step by Step into Spring(IOC)
- ife-task3:三栏式布局--再谈position 属性的基本知识
- April Fools Day Contest 2016 题解
- Spring MVC 常用的那些注解
- POJ1797 Heavy Transportation
- 《算法导论》学习摘要chapter-6——优先队列
- POJ_3253_Fence Repair
- [android] 图片的缩放
- maven导入本地jar文件
- 国内外 Android 技术大牛 入口
- 单例模式
- CSS Reset
- Linux内核分析——第十八章 调试
- Zookeeper + Hadoop2.6 集群HA + spark1.6完整搭建与所有参数解析
- Java学习路线图
- linux下lighttpdserver的具体安装步骤 以及对flv流媒体的支持配置
- 分布式存储概述
- Problem K: 反序数
- 剑指offer:找到二叉树中序遍历的下一个节点