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. SSM+Netty项目结合思路

    最近正忙于搬家,面试,整理团队开发计划等工作,所以没有什么时间登陆个人公众号,今天上线看到有粉丝想了解下Netty结合通用SSM框架的案例,由于公众号时间限制,我不能和此粉丝单独沟通,再此写一篇手记分 ...

  2. jQuery中$.ajax()方法参数解析

    本文实例为大家讲解了jQuery $.ajax()方法参数,供大家参考,具体内容如下 $.ajax({ url:'test.do', data:{id:123,name:'xiaoming'}, ty ...

  3. 使用Vue-Router 2实现路由功能

    转自:http://blog.csdn.net/sinat_17775997/article/details/54710420 注意:vue-router 2只适用于Vue2.x版本,下面我们是基于v ...

  4. 《锋利的jQuery》笔记:插件的使用和写法

    jQuery插件的种类 1.封装对象方法 这种插件是将对象方法封装起来,用于对通过选择器获取的jQuery对象进行操作,是最常见的一种插件.此类插件可以发挥出jQuery选择器的强大优势,有相当一部分 ...

  5. 2017-11-09 中文代码示例之Vuejs入门教程(一)

    "中文编程"知乎专栏原链 为了检验中文命名在主流框架中的支持程度, 在vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 所有演示都在本地测试通过, 源码在这里. 下面 ...

  6. iphone手机投屏在哪里 手机无线投屏电脑

    Iphone是我们经常使用的一款手机,有时候经常需要将一些文件图片信息等投屏到电脑,那么iphone手机投屏在哪里?可以无线投屏到电脑吗?其实很简单,下面就分享下苹果手机投屏的具体方法给大家,希望对大 ...

  7. Android为TV端助力 最详细的动画大全,包括如何在代码和在XML中使用

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...

  8. websocket 与 tornado 的结合

    对于socket是不陌生的,但是对于websocket我却是陌生的,不同于https,在网页中使用websocket可以同样起到ajax的作用,默默发送数据... 在script中: ws = new ...

  9. git 代码服务器的网页版gitweb的搭建

    sudo apt-get install apache2 git-core gitwebsudo a2enmod rewrite #vi /etc/gitweb.conf $projectroot = ...

  10. android build 编译打印详细过程

    我们在make otapackage编译android代码的时候,有时候需要跟踪详细的过程,包括所有的过程,可以修改build/core/Makefile,赋值hide := 为空即可