HDU 2821--Pusher
来源:互联网 发布:osx和linux 编辑:程序博客网 时间:2024/06/04 19:38
题目:这是题目
题意为:给一个矩阵,里面有一些方块和一个Pushboy,a,b,c...分别代表方块的数目为1,2,3...以此类推。目标就是把矩阵里面的方块都推完,有几个规则如下:
1. Pushboy和方块之间必须有一个空,也就是不能紧挨着。
2. 每次推一个木块,木块的数目会减1,并且剩余的木块会和当前方向的前一个方格的木块数合并,当然如果前一个方格是边界外,方块就全部推出。
最后要找到Pushboy的起始位置和推方块的方向路径。
之前wa了两发是题意中推了一次方块和不仅方块数量减1,而且位置要发生改变,没有注意到这个,后面wa了一发是dfs状态还原一不留神错了。
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <math.h>using namespace std;const int MAX = 30;int x[4] = {1, 0, 0, -1};int y[4] = {0, 1, -1, 0};char _map[MAX][MAX];int a[MAX][MAX];int num;int r, c;char path[700];int k;char Getpath(int i) { if (i == 0) return 'D'; else if (i == 1) return 'R'; else if (i == 3) return 'U'; else return 'L';}//判断走的方向,和前面的x和y数组有关bool dfs(int xx, int yy, int sum) { if (sum == num) return true; for (int i = 0; i < 4; i++) { int xi = xx + x[i]; int yi = yy + y[i]; if (xi >= 0 && xi < c && yi >= 0 && yi < r) { if (a[xi][yi] > 0) continue;//该方格有木块,不能推,往下一个方向 else { while (xi >= 0 && xi < c && yi >= 0 && yi < r) { xi += x[i]; yi += y[i]; if (a[xi][yi] > 0) { break; } } }//否则一直沿着该方向,走到有方块的那个方格 if (xi >= 0 && xi < c && yi >= 0 && yi < r) { int nm = a[xi][yi]; int xxx = xi+x[i]; int yyy = yi+y[i];//该方向有方块方格的前一个方格 if (xxx >= 0 && xxx < c && yyy >= 0 && yyy < r) { a[xi][yi] = 0; a[xxx][yyy] += nm-1; if (dfs(xi, yi, sum+1)) { path[k++] = Getpath(i);//记录路径 return true; } a[xi][yi] = nm; a[xxx][yyy] -= (nm-1); }//有方块方格的前一个方格有方块,则该方格方块数目为(本身的方块数+前一个方格的方块数-1) else { a[xi][yi] = 0; if (dfs(xi, yi, sum+nm)) { path[k++] = Getpath(i); return true; } a[xi][yi] = nm; }//前一个方格为边界外,直接清0 } } } return false;}int main() { while(scanf("%d%d", &r, &c) != EOF) { getchar(); memset(a, 0, sizeof(a)); num = 0; k = 0; for (int i = 0; i < c; i++) { for (int j = 0; j < r; j++) { scanf("%c", &_map[i][j]); if (_map[i][j] == '.') a[i][j] = 0; else { a[i][j] = _map[i][j] - 'a' + 1; num += a[i][j]; } } getchar(); } bool tag; for (int i = 0; i < c; i++) { tag = false; for (int j = 0; j < r; j++) { if (a[i][j] == 0) { if (dfs(i, j, 0)) { printf("%d\n%d\n", i, j); tag = true; break; } } } if (tag) break; } for (int i = k-1; i >= 0; i--) printf("%c", path[i]); printf("\n"); } return 0;}
0 0
- HDU 2821 Pusher
- DFS-hdu-2821-Pusher
- HDU 2821 Pusher
- HDU-2821-Pusher(DFS)
- HDU 2821 Pusher
- HDU 2821 Pusher
- HDU 2821--Pusher
- hdu 2821 Pusher DFS
- HDU 2821 Pusher
- hdu 2821 pusher 4.3.7
- hdu 2821 Pusher 附几组数据
- hdu 2821 Pusher (dfs)
- hdu 2821 Pusher(dfs)
- hdu 2821 Pusher (水dfs)
- 【搜索】 HDU 2821 Pusher DFS
- HDU 2821Pusher(DFS )
- hud-2821-Pusher
- (HDU 2821)Pusher DFS + 在一个方向上可以移动多位
- Android Studio Utf-8编码设置
- javaScript中Dom编程
- HDU 4460-BFS
- DjangoRestFramework实践笔记
- 威佐夫博弈
- HDU 2821--Pusher
- STL算法使用
- 数据结构与算法学习笔记——二叉树的初步理解
- 01背包
- 中点画圆法(计算机图形学)
- iOS 微信支付和支付宝支付
- [iOS]Mac下获取App Store安装包路径
- 10个JavaScript测试
- UI高级--------NSRunLoop