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. Hibernate入门(二)——hibernateAPI详解

    Hibernate API 详解 1.Configuration 功能:配置加载类,用于加载主配置,orm元数据加载 .创建: Configuration conf = new Configurati ...

  2. Java基础IO流(一)

    IO概念: 大多数应用程序都需要实现与设备之间的数据传输,例如键盘可以输入数据,显示器可以显示程序的运行结果等.在Java中,将这种通过不同输入输出设备(键盘,内存,显示器,网络等)之间的数据传输抽象 ...

  3. 【CSS学习】--- 文本水平对齐属性text-align和元素垂直对齐属性vertical-align

    一.文本水平对齐属性---text-align text-align属性是将块级标签以及单元格里面的内容进行相应的对齐,块级标签里的内联元素会被整体进行移动,而子块级元素或子单元格则会继承父元素的te ...

  4. html 获取数据并发送给后端方式

    一.方式一 使用ajax提交 function detailed() { var date = $("#asset_ip").text() $.ajax({ url: " ...

  5. python爬虫学习记录——各种软件/库的安装

    Ubuntu18.04安装python3-pip 1.apt-get update更新源 2,ubuntu18.04默认安装了python3,但是pip没有安装,安装命令:apt install py ...

  6. Charles 抓包手机app

    最近在测为移动端提供的API, 使用mac系统, 发现fiddler在mac下无法使用, 不知道其他朋友是否遇见过, 只能找替代工具. 先去百度上搜索下载Charles 破解版, 选择Charles是 ...

  7. spark大批量读取Hbase时出现java.lang.OutOfMemoryError: unable to create new native thread

    这个问题我去网上搜索了一下,发现了很多的解决方案都是增加的nproc数量,即用户最大线程数的数量,但我修改了并没有解决问题,最终是通过修改hadoop集群的最大线程数解决问题的. 并且网络上的回答多数 ...

  8. Python基于dtw实现股票预测【多线程】

    # -*- coding: utf-8 -*- """ Created on Tue Dec 4 08:53:08 2018 @author: zhen "&q ...

  9. SQL中常用日期函数

    --1 GETDATE() 返回当前系统日期SELECT GETDATE() --2 DATEADD(日期部分,常数,日期) 返回将日期的指定日期部分加常数后的结果返回 日期部分可以是: --常数为正 ...

  10. CSS杂谈(2)

    opacity 属性设置元素的不透明级别.   语法 opacity: value|inherit; 值 描述   value 规定不透明度.从 0.0 (完全透明)到 1.0(完全不透明).   i ...