您的位置:首页 > 其它

HDU 2821 Pusher

2013-07-09 11:18 423 查看
  原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821

  首先,题目描述给的链接游戏很好玩,建议先玩几关,后面越玩越难,我索性把这道题A了,也就相当于通关了。

  其实这道题算是比较水点搜索题,直接DFS + 回溯,但是题目描述不是很清楚使得很难下手,后来枚举题意才知道在边缘位置不会出现嵌套盒子,而且所有输入都肯定有解决的方案,所有的输入数据都是标准的。

#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int maxn = 30;

int dr[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char dd[4] = {'R', 'L', 'D', 'U'};
char g[maxn][maxn];
char path[25 * 25 * 10];
int a[maxn][maxn];
int r, c;
bool ok(int x, int y)
{
if(x >= 0 && x < c && y >= 0 && y < r && a[y][x] == 0)
return true;
return false;
}

int dfs(int cy, int cx, int cnt, int len)
{
if(len == cnt)
return 1;
for(int k = 0; k < 4; k++)
{
int x = cx + dr[k][0];
int y = cy + dr[k][1];
if(ok(x, y))
{
do
{
x += dr[k][0];
y += dr[k][1];
}
while(ok(x, y));
if(x >= 0 && x < c && y >= 0 && y < r)
{
int tmp = a[y][x];
a[y + dr[k][1]][x + dr[k][0]] += a[y][x] - 1;
a[y][x] = 0;
path[len] = dd[k];
if(dfs(y, x, cnt, len+1))
return 1;
path[len] = '\0';
a[y][x] = tmp;
a[y + dr[k][1]][x + dr[k][0]] -= a[y][x] - 1;
}
}

}
return 0;
}

int main()
{
int cnt;
while(scanf("%d %d", &c, &r) != EOF)
{
cnt = 0;
for(int i = 0; i < r; i++)
{
scanf("%s", g[i]);
for (int j = 0; j < c; j++)
{
if(g[i][j] != '.')
cnt += g[i][j] - 'a' + 1, a[i][j] = g[i][j] - 'a' + 1;
else
a[i][j] = 0;
}
}
int i, j;
bool flag = false;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
if(a[i][j] == 0 && dfs(i, j, cnt, 0))
{
flag = true;
break;
}
}
if(flag) break;
}
printf("%d\n%d\n", i, j);
for(int i = 0; i < cnt; i++) putchar(path[i]);
putchar('\n');
}
return 0;
}


View Code

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: