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. C#设计模式之七桥接模式(Bridge Pattern)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第二个模式,该模式是[桥接模式],也有叫[桥模式]的,英文名称:Bridge Pattern.大家第一次看到这个名称会想到什么呢?我第一次看到这个模式根据名称 ...

  2. CSS table-layout 属性

    设置表格布局算法: table { table-layout:fixed; } 所有浏览器都支持 table-layout 属性. 定义 tableLayout 属性用来显示表格单元格.行.列的算法规 ...

  3. PHP常用函数总结(二)

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  4. Oracle11g: simple sql script examples

    ---https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_8003.htm drop user geovin; drop ...

  5. BZOJ2746: [HEOI2012]旅行问题(AC自动机 LCA)

    Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1188  Solved: 383[Submit][Status][Discuss] Descripti ...

  6. DotNetBar的窗口样式丢失

    DotNetBar的窗口样式丢失 C# 调用DotNetBar很方便,将DevComponents.DotNetBar2.dll和DevComponents.DotNetBar.Design.dll放 ...

  7. 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)

    要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号   那么如何申请Office ...

  8. 14.Odoo产品分析 (二) – 商业板块(7) –制造(1)

    查看Odoo产品分析系列--目录 一旦你收到了库存中所需的原材料,就可以开始生产终端产品了.ERP系统的部分功能是帮助您根据可用资源调度这些订单.其中一项资源是原产品.其他资源可以包括可用劳动力或特定 ...

  9. WannaCry勒索比特币蠕虫病毒解决方案

    WannaCry ransomware used in widespread attacks all over the world Customer Guidance for WannaCrypt a ...

  10. p标签内容实现第二行缩进两个字体间距

    p{ word-break:normal; text-indent: -2em; margin-left: 2em;} <p> p标签实现自动换行:p标签实现自动换行:p标签实现自动换行: ...