题意:

给你一个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-骑士-搜索题的更多相关文章

  1. [HDU 2102] A计划(搜索题,典型dfs or bfs)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. UVa 489 HangmanJudge --- 水题

    UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...

  3. UVa 1585 Score --- 水题

    题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...

  4. bnuoj 33656 J. C.S.I.: P15(图形搜索题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...

  5. 历年NOIP中的搜索题

    什么题目都不会做于是开始做搜索题. 然而我搜索题也不会做了. 铁定没戏的蒟蒻. 1.NOIP2004 虫食算 “对于给定的N进制加法算式,求出N个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...

  6. poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]

    地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...

  7. POJ-2386Lake Counting,搜索题。。

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K           Description Due to recent rains, w ...

  8. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  9. hdu&&poj搜索题题号

    搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...

  10. codevs 搜索题汇总(青铜+白银级)

    1792 分解质因数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze   题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描 ...

随机推荐

  1. Mac OS安装Windows各版本时注意事项(2014年后的Mac机相信会有这些问题)

    2014年后的Mac Mini安装Windows时候,会遇上更种坑,我这几天不断失败及尝试更种版本,各有心得体会,我写下来是为了避免大家遇到像我这种问题. (2014年之前的Mac Mini是否这样, ...

  2. Excel操作小结

    插入下拉选择(例如类型):选中单元格==>数据有效性==>数据有效性==>设置/有效性条件==>系列(下拉框内容用英文逗号分开): 插入下拉框设置不同背景色:选择需要设置的单元 ...

  3. BlockingQueue队列

    1.BlockingQueue定义的常用方法如下     抛出异常 特殊值 阻塞 超时 插入 add(e) offer(e) put(e) offer(e,time,unit) 移除 remove() ...

  4. windows cmd下ssh连接免密码问题解决

    windows 7 cmd下 ssh -T username@serverip 免密码连接成功 有的同学在windows下开发,并且在windows下安装了git for windows,这些资源已经 ...

  5. Ubuntu 14.10 下HBase错误集

    1 如果机群时间不同步,那么启动子节点RegionServer就会出问题 aused by: org.apache.hadoop.hbase.ipc.RemoteWithExtrasException ...

  6. ubuntu16.04安装chrome

    方法1: 到 https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 下载最新的安装文件. 然后使用cd命令 ...

  7. 【IIS错误】IIS各种错误

    IIS简介 当用户试图通过HTTP或文件传输协议(FTP)访问一台正在运行Internet信息服务 (IIS)的服务器上的内容时,IIS返回一个表示该请求的状态的数字代码.该状态代码 记录在IIS日志 ...

  8. PAT 乙级 1017 A除以B (20) C++版

    1017. A除以B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求计算A/B,其中A是不超过 ...

  9. Linux环境下配置maven环境

    1.下载安装包并解压 安装包直接去apache官网下载:https://maven.apache.org/download.cgi 将安装包放在自己指定的目录:/home/software/apach ...

  10. 提取字符串substring()

    substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法: stringObject.substring(startPos,stopPos)  参数说明: 注意: 1. 返回的内 ...