uva-10422-骑士-搜索题
题意:
给你一个5X5的图,棋盘上骑士的走法自己去百度,问能不能在10步内走到目标图,
解题思路:
从目标图开始往前走10步,保存所有能走到的图,然后输入,查找是否存在这个图,存在就是可以走到,不存在,走不到
使用map集合保存状态图,优先队列对状态重排序
#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
#include <queue> using namespace std; struct Dir
{
int x, y;
}; struct Node
{
int x,y;
string str;
int step;
bool operator () (const Node& n1,const Node& n2)const
{
return n1.step>n2.step;
}
};
//使用优先队列对状态重排序
priority_queue<Node, vector<Node>, Node> q; Dir dir[] =
{
{ +, - },
{ +, + },
{ +, - },
{ -, + },
{ -, + },
{ -, - },
{ -, - },
{ +, + }
}; map<string, int> maps;
const int N=; string nextStr(int cr, int cc, int r, int c, string cur)
{
string str(cur);
char curc = cur[cr * N + cc];
char nextc = cur[r * N + c];
str[cr * N + cc] = nextc;
str[r * N + c] = curc;
return str;
} void bfs()
{
while(q.empty()==false)
{
Node n = q.top();
q.pop();
string cur = n.str;
int depth = n.step;
int cr = n.x;
int cc = n.y;
//已经到10步
if(depth==)
return;
for(int i = ; i < ; i++)
{
//nx row
int r = dir[i].x + cr;
//col
int c = dir[i].y + cc;
if(r < || c < || r >= N || c >= N)
continue;
string str=nextStr(cr, cc, r, c, cur);
if(maps.find(str)!=maps.end())
continue;
maps[str] = depth+;
Node nNode;
nNode.x=r;
nNode.y=c;
nNode.step=depth+;
nNode.str=str;
q.push(nNode);
}
}
} int main()
{
freopen("d://1.text", "r", stdin);
//5x5图
//目标图
string aim =
""
""
"00 11"
""
"";
Node iNode;
iNode.step=;
iNode.str = aim;
iNode.x = ;
iNode.y = ;
q.push(iNode);
maps[aim] = ;
bfs();
int total;
cin >> total;
getchar();
while (total--)
{
string str = "";
string inStr = "";
for(int i = ; i < ; i++)
{
getline(cin, inStr);
str += inStr;
}
map<string, int>::iterator s;
int ok = ;
for(s = maps.begin(); s != maps.end(); ++s)
{
if(s->first == str)
{
ok=;
cout<<"Solvable in "<<s->second<<" move(s)."<<endl;
break;
}
}
if(!ok)
cout<<"Unsolvable in less than 11 move(s)."<<endl; } return ;
}
uva-10422-骑士-搜索题的更多相关文章
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
- UVa 1585 Score --- 水题
题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...
- bnuoj 33656 J. C.S.I.: P15(图形搜索题)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...
- 历年NOIP中的搜索题
什么题目都不会做于是开始做搜索题. 然而我搜索题也不会做了. 铁定没戏的蒟蒻. 1.NOIP2004 虫食算 “对于给定的N进制加法算式,求出N个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...
- poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]
地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...
- POJ-2386Lake Counting,搜索题。。
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, w ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- hdu&&poj搜索题题号
搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...
- codevs 搜索题汇总(青铜+白银级)
1792 分解质因数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描 ...
随机推荐
- vivado各版本的区别
vivado各版本的区别 Vivado HL Design Edition: Vivado HL System Edition: 区别在于System Edition包含有system generat ...
- selenium phantomjs 设置代理ip方法
最近遇到phantomjs动态更换ip的功能,在知乎上看到一篇不错的文章,顺手记下来以备后用 phantomjs selenium 如何动态修改代理? 可以这样做(Python代码): # 不使用代理 ...
- 跟未名学Office - PPT核心:表达
目录 第一章. PPT核心:表达 2 第一节 观点 2 第二节 数据来源 2 第三节 逻辑顺序 3 PPT核心:表达 观点 1 vs N 要表达什么? 为什么要做成 ...
- 黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素
原文:http://www.solagirl.net/customize-wordpress-media-upload-ui.html WordPress编写文章界面的添加媒体按钮允许用户上传多媒体文 ...
- Jenkins的详细安装
操作环境:Windows 一.环境准备 1 安装JDK 本文采用jdk-8u111-windows-x64.exe: 2 配置tomcat 本文采用tomcat8,无需安装,配置JAVA_HOME及J ...
- 关于java多线程理解到集群分布式和网络设计的浅析
对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题, 另外关于java多线程的知识也是非常的多,本文中先介绍和说明一 ...
- 【linux】之内核升级
安装docker要满足一定的条件,对于cents系统,要求必须是64位,并且内核版本是3.10以上. 如果你的centos操作系统内核低于3.10,需要升级到这个版本以上,才能安装docker. 第一 ...
- uoj#29. 【IOI2014】Holiday
http://uoj.ac/problem/29 经过的点集一定是一个包含start的区间,为了经过这个区间内所有点,必须先到达一个区间端点,再到达另一个区间端点,剩余的步数则贪心选区间内最大价值的点 ...
- 《Java并发编程实战》笔记-OneValueCache与原子引用技术
/** * NumberRange * <p/> * Number range class that does not sufficiently protect its invariant ...
- kickstart
关闭防火墙.关闭selinux 1.配置DHCP服务 # yum install dhcp -y dhcp配置文件如下 # vi /etc/dhcp/dhcpd.conf 查看路径 # rpm -ql ...