袋鼠妈妈找孩子
Time limit per test: 1.5 seconds
Time limit all tests: 10.0 seconds
Memory limit: 256 megabytes
袋鼠妈妈找不到她的孩子了。她的孩子被怪兽抓走了。
袋鼠妈妈现在在地图的左上角,她的孩子在地图第 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
.*.*** ......
Source
2017 华东师范大学网赛
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
char ch[15][15];
int n, m;
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int dfs(int x, int y, int step,int limit)
{
if (x == 1 && y == 1 && step >= limit) return true;
for (int i = 0; i < 4; i++)
{
int xx = x + dir[i][0], yy = y + dir[i][1],sum=0;
if (xx <= 0 || yy <= 0 || xx > n || yy > m||ch[xx][yy]!='*') continue;
for (int j = 0; j < 4; j++)
if (ch[xx + dir[j][0]][yy + dir[j][1]] == '.') sum++;
if (sum > 1) continue;
ch[xx][yy] = '.';
if (dfs(xx, yy, step + 1, limit)) return true;
ch[xx][yy] = '*';
}
return false;
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
memset(ch, 0, sizeof ch);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ch[i][j] = '*';
int x, y, k;
scanf("%d%d%d", &x, &y, &k);
ch[x][y] = '.';
dfs(x, y, 0,k);
for (int i = 1; i <= n; i++)
printf("%s\n", ch[i] + 1);
}
return 0;
}