您的位置:首页 > 其它

EOJ 3260 大学生程序设计邀请赛(华东师范大学)-C 袋鼠妈妈找孩子

2017-05-22 22:12 302 查看

题目

http://acm.ecnu.edu.cn/problem/3260/

题意

袋鼠妈妈找不到她的孩子了。她的孩子被怪兽抓走了。

袋鼠妈妈现在在地图的左上角,她的孩子在地图第 x 行第 y 列的位置。怪兽想和袋鼠妈妈玩一个游戏:他不想让袋鼠妈妈过快地找到她的孩子。袋鼠妈妈每秒钟可以向上下左右四个方向跳一格(如果没有墙阻拦的话),怪兽就要在一些格子中造墙,从而完成一个迷宫,使得袋鼠妈妈能够找到她的孩子,但最快不能小于 k 秒。

请设计这样一个迷宫。

Input

第一行两个整数 n,m (1≤n,m≤8),表示地图的总行数和总列数。

第二行三个整数 x,y,k (1≤x≤n,1≤y≤m,x+y>1)。

Output

输出一个地图,应正好 n 行 m 列。

用 . 表示空地,用 * 表示墙。袋鼠妈妈所在的位置和孩子所在的位置用 . 表示。

数据保证有解。

Examples

input

2 6

1 3 4

output

..**

……

思路

搜索构造题。从起点开始搜索,当前在i点,向i点四个方向中的某一个转移的时候,把另外三个方向打上’*’,防止从另外三个方向有到达终点更近的路线。就这样一直搜到终点,判断是否大于等于k秒,之道搜到大于等于k秒的为止。

#include <bits/stdc++.h>
using namespace std;

const int N = 10 + 10;

char str

;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m, k, rx, ry;
bool found;
bool vis

;

bool check(int x, int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m && str[x][y] != '*' && vis[x][y] == false) return true;
else return false;
}
void dfs(int x, int y, int step)
{
if(found) return;
if(x == rx && y == ry && step >= k)
{
for(int i = 1; i <= n; i++) puts(str[i]+1);
found = true;
return;
}
int tx[4], ty[4], tot = 0;
for(int i = 0; i < 4; i++)//tx记录当前位置四个方向中合法的方向
{
int nx = x + dx[i], ny = y + dy[i];
if(check(nx, ny)) tx[tot] = nx, ty[tot++] = ny;
}
for(int i = 0; i < tot; i++)//枚举应该走哪个方向
{
for(int j = 0; j < tot; j++)//除了当前被枚举方向,其他方向均打上'*'
{
if(j == i) continue;
else str[tx[j]][ty[j]] = '*';
}

vis[tx[i]][ty[i]] = true;
dfs(tx[i], ty[i], step + 1);
vis[tx[i]][ty[i]] = false;

for(int j = 0; j < tot; j++)//消除标记
{
if(j == i) continue;
else str[tx[j]][ty[j]] = '.';
}
}
}
int main()
{
scanf("%d%d%d%d%d", &n, &m, &rx, &ry, &k);
memset(str, 0, sizeof str);
memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
str[i][j] = '.';
//for(int i = 1; i <= n; i++) puts(str[i]+1);
found = false;
vis[1][1] = true;
dfs(1, 1, 0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐