hdu2102 A计划

12 篇文章 0 订阅

题目链接:

here

分析:

遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行。。还有遇到 一层是#一层是墙的。。也直接把俩都弄城墙就行。。。省的要判断他撞死。。哈哈、、我遇到了点小问题。。。就是遇到#的时候,我忘了加步数。。。所以一直wa。。最后才检查出来。。。囧。。。。。低级失误。。。虽然上下楼不用时间。。但是你走到#需要一步的时间。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define inf (0x7f7f7f7f)
const int maxn = 12;

int c, n, m, t;
char map[2][maxn][maxn];
bool vis[2][maxn][maxn];
struct node
{
	int x, y, z;
	int step;
};
queue<node> q;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

bool judge(int x, int y)
{
	if (x < 0 || y < 0 || x >= n || y >= m) return false;
	return true;
}

bool bfs()
{
	bool flag = false;
	while (!q.empty())
	{
		node now = q.front();
		q.pop();

		if (map[now.z][now.x][now.y] == 'P')
		{
			if (now.step >= 0)
				return true;
			else
				break;
		}

		node next;
		for (int i=0; i<4; i++)
		{
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			next.z = now.z;
			next.step = now.step - 1;

			if (!judge(next.x,next.y)) continue;

			if (map[now.z][next.x][next.y] == '#')
				next.z ^= 1;
			
			if (map[next.z][next.x][next.y] != '*' && next.step >= 0 && !vis[next.z][next.x][next.y])
			{
				vis[next.z][next.x][next.y] = true;
				q.push(next);
			}
		}
	}
	return false;
}

int main()
{
//	freopen("2102.in", "r", stdin);
//	freopen("2102.out", "w", stdout);

	scanf("%d", &c);
	while (c--)
	{
		scanf("%d %d %d", &n, &m, &t);
		memset(vis, false, sizeof(vis));
		while (!q.empty()) q.pop();

		int i, j, k;
		for (i=0; i<2; i++)
			for (j=0; j<n; j++)
				scanf("%s", map[i][j]);
		node s;

		for (i=0; i<2; i++)
			for (j=0; j<n; j++)
				for (k=0; k<m; k++)
				{
					if (map[i][j][k] == 'S')
						s.x = j, s.y = k, s.z = i, s.step = t;
					else if (map[i][j][k] == '#' && map[1^i][j][k] == '#')
					{
						map[i][j][k] = '*';
						map[1^i][j][k] = '*';
					}
					else if (map[i][j][k] == '#' && map[1^i][j][k] == '*')
						map[i][j][k] = '*';
				}
		vis[s.z][s.x][s.y] = true;
		q.push(s);
		if (bfs())
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}

测试数据:

33
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

5 5 13
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..


5 5 12
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

1 2 100
SP

..

1 3 100
S#P

.#.


3 3 11
S..
*#.
#..

...
.#.
.*P




5 5 11
S*#*.
.#...
.....
****.
P.*#.

..*..
#.*..
***..
...*.
*#.#.

5 5 12
S*#*.
.#...
.....
****.
P.*#.

..*..
#.*..
***..
...*.
*#.#.

5 5 12
S*#*.
.#...
.....
****.
P.*#.

..*..
#.*..
***..
...*.
*.##.

5 5 3
S*#*.
.#...
.....
****.
..*#.

.P*..
#.*..
***..
...*.
*#.#.

5 5 9
S*#*.
.#...
.....
****.
..*#.

..*..
#.*..
***..
...*.
*#.P.

4 5 7
S*#*.
...**
.#.**
****.

.*.*.
.#*..
#.#..
.#.*P

5 7 12
S*****P
.#.*#*.
**.#.*.
.*****.
.......

*******
*.#*...
***.#*#
*******
*******

3 3 3
S#P
.*.
...

.##
...
...

输出数据:

YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
^o^

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值