【EOJ 3260. 袋鼠妈妈找孩子】(dfs,构造)
2018-02-12 17:05
459 查看
Time limit per test: 2.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步,只要找到一条路径使得(0,0)到(tx,ty)距离dis不小于k即可。 既然找最小的步数, 这里隐含着 要避免走弯路。 即一个点向四个方向扩张的步数step要小于2。 dfs搜索一遍
【dfs】
代码:
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步,只要找到一条路径使得(0,0)到(tx,ty)距离dis不小于k即可。 既然找最小的步数, 这里隐含着 要避免走弯路。 即一个点向四个方向扩张的步数step要小于2。 dfs搜索一遍
【dfs】
代码:
#include <cstdio> #include <iostream> #include <cstring> #include <map> #include <set> #include <bitset> #include <cctype> #include <cstdlib> #include <queue> #include <cmath> #include <stack> #include <ctime> #include <string> #include <vector> #include <sstream> #include <functional> #include <algorithm> using namespace std; #define mem(a,n) memset(a,n,sizeof(a)) #define memc(a,b) memcpy(a,b,sizeof(b)) #define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n) #define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a] #define pb push_back #define fi first #define se second #define IO ios::sync_with_stdio(false) #define fre freopen("in.txt","r",stdin) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; typedef unsigned long long ull; const double PI=acos(-1.0); const double E=2.718281828459045; const double eps=1e-8; const int INF=0x3f3f3f3f; const int MOD=258280327; const int N=1e3+5; const ll maxn=1e6+5; const int dir[4][2]= {-1,0,1,0,0,-1,0,1}; char a ; bool vis ; int n,m,tx,ty,k; bool in(int x,int y) { if(x>=0&&x<n&&y>=0&&y<m) return true; return false; } bool dfs(int x,int y,int dis) { if(x==tx&&y==ty) return dis>=k; rep(i,0,4) { int nx=x+dir[i][0]; int ny=y+dir[i][1]; if(in(nx,ny)&&!vis[nx][ny]) { int step=0; rep(k,0,4) { int nnx=nx+dir[k][0]; int nny=ny+dir[k][1]; if(in(nnx,nny)&&vis[nnx][nny]) step++; } if(step>=2) continue; vis[nx][ny]=1,a[nx][ny]='.'; if(dfs(nx,ny,dis+1)) return true; vis[nx][ny]=0,a[nx][ny]='*'; } } return false; } int main() { //fre; while(~scanf("%d%d%d%d%d",&n,&m,&tx,&ty,&k)) { tx--,ty--; mem(vis,0),mem(a,'*'); vis[0][0]=1,a[0][0]='.'; dfs(0,0,0); rep(i,0,n) { a[i][m]=0; puts(a[i]); } } return 0; }
相关文章推荐
- EOJ 3260:袋鼠妈妈找孩子
- EOJ 3260 袋鼠妈妈找孩子
- EOJ-大学生程序设计邀请赛(华东师范大学)-C-袋鼠妈妈找孩子
- ecnu 3260 袋鼠妈妈找孩子
- EOJ 3260 袋鼠妈妈找孩子 (DFS)
- EOJ 3260 袋鼠妈妈找孩子(dfs路径查找(过程优化))
- 大学生程序设计邀请赛(华东师范大学)C:袋鼠妈妈找孩子
- EOJ 3260 袋鼠妈妈找孩子 题解
- EOJ 3260 大学生程序设计邀请赛(华东师范大学)-C 袋鼠妈妈找孩子
- Codeforces Round #261 (Div. 2)(树状数组,dp,dfs构造序列)
- hihocoder 1192 简单的树嵌入 (树上DFS 构造 好题)
- 软件架构师必备素质 架构是厂长、开发是妈妈、产品经理是爸爸、测试是医生、产品是孩子、小姨子是培训师、护士是运维
- POJ 1426 Find The Multiple (DFS + 构造)
- LeetCode 22 Generate Parentheses (DFS 构造)
- 习题 6-14 UVA - 12118 Inspector's Dilemma 检察员的难题 (DFS 构造欧拉通路)
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- 这样的妈妈绝对教出优秀的孩子(留着学习用)
- HDU 4850 (构造 dfs)
- USACO Hamming Codes DFS 构造
- 孩子跟妈妈一起去按摩