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. mysql日期时间函数

    日期时间函数1.取得当前日期时间SELECT NOW(), SYSDATE(),CURRENT_TIMESTAMP() FROM DUAL2.取得当前日期SELECT CURDATE(),CURREN ...

  2. Codeforces389D(SummerTrainingDay01-J)

    D. Fox and Minimal path time limit per test:1 second memory limit per test:256 megabytes input:stand ...

  3. crontab 配置文件

    1.系统配置文件 etc/crontab 2.vim打开crontab 以上配置解释 1. 代表用bash去执行shell command line2.代表crontab 默认的环境变量3.cront ...

  4. FE 命令随笔

    FE_CMD ————— * >>>>>>>> Vue ________________________________________________ ...

  5. elementUI vue 页面加载的时候页面出现了黑字 页面优化处理 按钮弹出框文字

    elementUI 页面如果需要加载很多东西的时候, 自己定义的按钮或者弹出框dialog的文字就会显示在页面上, 一闪而过, 因此需要优化一下, elementUI 提供的loading有遮罩层, ...

  6. 【代码笔记】Web-JavaScript-javascript while循环

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  7. 一位ML工程师构建深度神经网络的实用技巧

    一位ML工程师构建深度神经网络的实用技巧 https://mp.weixin.qq.com/s/2gKYtona0Z6szsjaj8c9Vg 作者| Matt H/Daniel R 译者| 婉清 编辑 ...

  8. Android笔试题三

    1.java堆得Young区由哪些组成: Java堆由Perm区和Heap区组成,Heap区由Old区和New区(也叫Young区)组成,New区由Eden区.From区和To区(Survivor)组 ...

  9. js数组的用法以及数组根据下标(数值或字符)移除元素

    1.创建数组var array = new Array();var array = new Array(size);//指定数组的长度var array = new Array(item1,item2 ...

  10. (网页)jquery-qrcode.js生成二维码

    基础用法 <div id="qrcode"></div> <script type="text/javascript"> n ...