题意:

给你一个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. 阅读《7 Series FPGAs GTX/GTH Transceivers User Guide》

    阅读<7 Series FPGAs GTX/GTH Transceivers User Guide> 1.GTX在XC7K325T芯片内的排列 2.参考时钟的配置 在GTXE2_COMMO ...

  2. MYSQL优化浅谈,工具及优化点介绍,mysqldumpslow,pt-query-digest,explain等

    MYSQL优化浅谈 msyql是开发常用的关系型数据库,快速.稳定.开源等优点就不说了. 个人认为,项目上线,标志着一个项目真正的开始.从运维,到反馈,到再分析,再版本迭代,再优化… 这是一个漫长且考 ...

  3. Delphi中使用ADO连接Excel

    第一部分: . 设置ADOConnection的ConnectionString属性的OLE DB的提供者要选择Microsoft Jet 4.0 OLE DB Provider(这本来是用于连接Ac ...

  4. Python 环境的搭建(转载)

    原文来自 http://www.cnblogs.com/windinsky/archive/2012/09/20/2695520.html 1.首先访问http://www.python.org/do ...

  5. 黄聪:在.NET中使用GeckoFX 29

    GeckoFX is a .NET control, that works similarly to “System.Windows.Forms.WebBrowser” Control, while ...

  6. HAAR小波

    HAAR小波分解信号或图像的“平滑”部分和“变化”部分(也许所有小波都这样?). 比如信号[1 2 3 4 5 6 7 8] 分解后(不考虑系数): [1.5 3.5 5.5 7.5]         ...

  7. Python selenium —— selenium与自动化测试成神之路

    From: https://blog.csdn.net/huilan_same/article/details/52559711 忽然想谈谈自动化的学习路径,因为发现很多人总是急于求成,不懂该如何学习 ...

  8. 学习笔记之深度学习(Deep Learning)

    深度学习 - 维基百科,自由的百科全书 https://zh.wikipedia.org/wiki/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0 深度学习(deep lea ...

  9. Zabbix 创建监控项目

    #1 #2 [root@nod01 zabbix_agentd.d]# pwd/etc/zabbix/zabbix_agentd.d 新建文件nod.conf [root@nod01 zabbix_a ...

  10. intent--Activity之间数据传递之Intent数据传递

    intent传值: 4,intent传集合 3,intent传对象, 2,传递后有返回值的情况:当需要从目标Activity回传数据到原Activity时,可以使用上述方法定义一个新的Intent来传 ...