Problem UVA10384-The Wall Pushers

Accept: 199   Submit: 1546
Time Limit: 10000 mSec

Problem Description

Input

The input file may contain several mazes to solve. Each maze description starts with a single line containing two integers x and y (1 ≤ x ≤ 6,1 ≤ y ≤ 4) which is the start position in the maze. Next follows four lines with six integers each. These integers p (0 ≤ p ≤ 15) describe each square in the maze in the following way: p is the sum of 1 (if there is a wall west of the square), 2 (north), 4 (east) and 8 (south). Each inner wall will thus be mentioned twice. Each opening in the boundary is considered an exit. The input ends with a maze with starting coordinates 0, 0 and should not be processed.

 Output

Output a single line for each maze with the description of a path with minimum length that leads to any of exits. Use the letters ‘N’, ‘S’, ‘E’ and ‘W’ to denote north, south, east and west, respectively. If there are several solutions with minimum length, display any one of them.

 

 Sample Input

2 3
10 2 10 10 2 6
3 12 11 14 9 4
13 15 3 6 15 13
14 11 12 9 14 11
0 0
 

 Sample Output

NESESEENNWNWWWWW

题解:这个题最大的提示在Input里,直接告诉你最佳存图方式,最短路径,因此采用IDA*,之后就是水题了。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = , maxm = ;
const char direction[] = "WNES";
const int INF = 0x3f3f3f3f;
const int dx[] = { ,-,, };
const int dy[] = { -,,, };
const int dir[] = { ,,, }; int sx, sy, maxd;
int ans[maxn*maxm];
int gra[maxn][maxm];
bool vis[maxn][maxm]; vector< pair<int, int> > Exit; int ok(int x,int y) {
if (x == && !(gra[x][y] & )) return ;
else if (x == && !(gra[x][y] & )) return ;
if (y == && !(gra[x][y] & )) return ;
else if (y == && !(gra[x][y] & )) return ;
return -;
} bool Judge(int x, int y) {
if (x < || y < || x > || y > ) return true;
return false;
} bool dfs(int d, int x, int y) {
if (d == maxd) return false;
int tmp = ok(x, y);
if (tmp != -) {
ans[d] = tmp;
return true;
} int h = INF;
for (vector< pair<int, int> >::iterator it = Exit.begin(); it != Exit.end(); it++) {
h = min(h, abs(it->first - x) + (it->second - y));
}
if (d + h > maxd) return false; for (int i = ; i < ; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (Judge(xx, yy) || vis[xx][yy]) continue;
if (!(gra[x][y] & dir[i])) {
vis[xx][yy] = true;
ans[d] = i;
if (dfs(d + , xx, yy)) return true;
vis[xx][yy] = false;
}
else if (!(gra[xx][yy] & dir[i])) {
gra[xx][yy] += dir[i];
gra[x][y] -= dir[i];
if (!Judge(xx + dx[i], yy + dy[i])) {
gra[xx + dx[i]][yy + dy[i]] += dir[(i + ) % ];
}
ans[d] = i;
vis[xx][yy] = true;
if (dfs(d + , xx, yy)) return true;
vis[xx][yy] = false;
if (!Judge(xx + dx[i], yy + dy[i])) {
gra[xx + dx[i]][yy + dy[i]] -= dir[(i + ) % ];
}
gra[xx][yy] -= dir[i];
gra[x][y] += dir[i];
}
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d", &sx, &sy) && (sx || sy)) {
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
scanf("%d", &gra[i][j]); if (j == ) {
if (gra[i][j] & ) Exit.push_back(make_pair(i, j));
}
else if (j == ) {
if (gra[i][j] & << ) Exit.push_back(make_pair(i, j));
}
}
if (i == ) {
for (int j = ; j <= ; j++) {
if (gra[i][j] & ( << )) Exit.push_back(make_pair(i, j));
}
}
else if (i == ) {
for (int j = ; j <= ; j++) {
if (gra[i][j] & ( << )) Exit.push_back(make_pair(i, j));
}
}
} for (maxd = ;; maxd++) {
memset(vis, false, sizeof(vis));
vis[sy][sx] = true;
if (dfs(, sy, sx)) break;
} for (int i = ; i < maxd; ++i) {
printf("%c", direction[ans[i]]);
}
printf("\n");
}
}

UVA10384-The Wall Pushers(迭代加深搜索)的更多相关文章

  1. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  2. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  3. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  4. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  5. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  8. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

  9. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

  10. UVA11212-Editing a Book(迭代加深搜索)

    Problem UVA11212-Editing a Book Accept:572  Submit:4428 Time Limit: 10000 mSec  Problem Description ...

随机推荐

  1. Laravel5.5 数据库迁移:创建表与修改表

    数据库迁移是数据库的版本管理,要使用数据库迁移,需要在.env文件中连接好数据库(不多说).laravel本身已经存在user表和password_resets表的迁移了,因此,执行 php arti ...

  2. 写一个可插入自定义标签的 Textarea 组件

    - “插入自定义标签是什么鬼?” - “比如你要插入一个<wise></wise>的标签...” - “什么情况下会有这种需求?” - “得罪了产品的情况下...” 一.需求背 ...

  3. next.js学习笔记

    github地址: https://github.com/zeit/next.js#fetching-data-and-component-lifecycle 简介 Next.js是一个用于React ...

  4. Markdown简单上手

    标题 # +内容 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 字体 1. 加粗(Ctrl+B) **加粗** 2. 斜体(Ctrl+I) *斜体* 3. 斜体加粗(Ctrl+B+I) ...

  5. Ehcache配置详解及CacheManager使用

    <?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://w ...

  6. node+pm2+express+mysql+sequelize来搭建网站和写接口

    前面的话:在这里已经提到了安装node的方法,node是自带npm的.我在技术中会用es6去编写,然后下面会分别介绍node.pm2.express.mysql.sequelize.有少部分是摘抄大佬 ...

  7. AI在汽车中的应用:实用深度学习

    https://mp.weixin.qq.com/s/NIza8E5clC18eMF_4GMwDw 深度学习的“深度”层面源于输入层和输出层之间实现的隐含层数目,隐含层利用数学方法处理(筛选/卷积)各 ...

  8. 13张动图助你彻底看懂马尔科夫链、PCA和条件概率!

    13张动图助你彻底看懂马尔科夫链.PCA和条件概率! https://mp.weixin.qq.com/s/ll2EX_Vyl6HA4qX07NyJbA [ 导读 ] 马尔科夫链.主成分分析以及条件概 ...

  9. MySQL 横向表分区之RANGE分区小结

    MySQL 横向表分区之RANGE分区小结 by:授客 QQ:1033553122 目录 简介 1 RANGE分区 1 创建分区表 1 查看表分区 2 新增表分区 2 新增数据 3 分区表查询 3 删 ...

  10. Android 源码编译之旅

    目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...