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分解为质因数乘积的程序. 输入描 ...
随机推荐
- ecstore-自定义app里增加对应的wap的模板
注意第二个参数空第三个参数是自定义app名称$this->page('wap/member/recmembers.html','','ecstoreapprcomp2p');
- [转]Sublime Text 新建文件快速生成Html【头部信息】和【代码补全】、【汉化】
Sublime Text 新建文件快速生成Html[头部信息]和[代码补全].[汉化] 真心越来越喜欢sublime 这个工具,高效便捷,渐渐离不了了! 安装package control简单的安装方 ...
- 写了一个RenderInBackground的脚本
某主管希望write节点有多线程渲染的功能,而nuke中的render in background功能恰恰可以多个渲染任务同时执行,于是我考虑使用这个方法来实现. 调 试过程中发现renderinba ...
- RTB业务知识之1-原生广告
一.背景 Native Advertising (Native Ads), 又称为原生广告, 是2013全球媒体界爆红的关键词,从2012年年底,就有人开始提了这个名词,接着到处都可以看到这个名词,再 ...
- draftsight的热补丁
http://www.piaodown.com/soft/134200.htm DraftSight HotFix 2017R3热修复补丁下载.DraftSight,一个非常好用的2D制图软件,由开发 ...
- bzoj3491: PA2007 Subsets
Description 有一个集合U={1,2,…,n),要从中选择k个元素作为一个子集A.若a∈A,则要有a*X不属于A,x是一个给定的数.求可选方案对M取模后的值. 1< = N< = ...
- C#应用jstree实现无限级节点的方法
下载jstree.js下载地址: http://jstree.com/ 当前下载版本: jsTree 3.3.1 第一步:下载完成后引用js+css <link href="~/plu ...
- java中友元的完美实现(转载)
http://blog.sina.com.cn/s/blog_ce5b9f7e0102vhiv.html C++中有一个友元机制,可以给予一个类访问另一个类中私有成员的权限. 这个机制有时候是很有 ...
- centos7部署openvpn-2.4.6
一.环境说明 返回主机的IP地址 # ip a | grep "scope global" | awk -F'[ /]+' '{print $3}' | head -1 [root ...
- [UE4]小地图UI设计
一.新建一个名为TestMiniMap的UserWidget用来使用小地图StaticMiniMap. 二.在左侧“User Created”面板中可以看到除自身以外的其他所有用户创建的UserWid ...