袋鼠媽媽找孩子
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
..**
……
題意:構造一個步數超過k的通路到達目標點。
思路:還是菜啊,。。看了題解想了想懂了。。我們只需要找到一條距離超過k的就可以了,那麼如果我當前位置要往前走,對走到的那個格子來說,如果只有之前那個格子可以走,那麼必定是可行的,這是個充分條件,滿足這個條件必定是可以的,但是你在旁邊再加格子不一定不可行。碼的時候注意,步數是不小於k。。我寫成了等於。。賊迷。。還有圖上可走是*不是#…這裏我也錯了。。
不懂的話可以調用我的debug看看。。。反正就是從一堆不能走的路走出一條長度大於k的路2333
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;
//thanks to pyf ...
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e6 + 5;
char Map[10][10];
int vis[10][10][100];
int xdir[4] = {0, 1, 0, -1};
int ydir[4] = {1, 0, -1, 0};
int k = 0;
int tx, ty;
int flag = 0;
int n, m;
void init()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Map[i][j] = i == 0 && j == 0 ? '.' : '*';
}
}
}
bool judge(int x, int y)
{
int cnt = 0;
for (int i = 0; i < 4; i++)
{
int tx = x + xdir[i] ;
int ty = y + ydir[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m)
continue;
if (Map[tx][ty] == '.')
cnt++;
}
return cnt == 1;
}
void debug()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << Map[i][j];
}
cout << endl;
}
cout << endl;
}
void dfs(int i, int j, int step)
{
if (flag)
return;
Map[i][j] = '.';
if (i == tx && j == ty)
{
if (step >= k)
{
flag = 1;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < m; y++)
cout << Map[x][y];
cout << endl;
}
}
Map[i][j] = '*';
return ;
}
// debug();
for (int k = 0; k < 4; k++)
{
int tx = i + xdir[k];
int ty = j + ydir[k];
if (tx < 0 || tx >= n || ty < 0 || ty >= m || Map[tx][ty] == '.')
continue;
if (judge(tx, ty))
dfs(tx, ty, step + 1);
}
Map[i][j] = '*';
}
int main()
{
while (cin >> n >> m)
{
flag = 0;
cin >> tx >> ty >> k;
tx--, ty--;
init();
flag = 0;
dfs(0, 0, 0);
}
}