题意:

给你一个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. 基于OLSR的路由协议实现Ad-Hoc组网

    一.软件包的安装 1. olsrd软件包的安装 libpthread_0.9.33.2-1_ar71xx.ipk olsrd_0.6.6.2-4_ar71xx.ipk 2. luci的安装 olsrd ...

  2. 外同步信号检测---verilog---状态机

    外同步信号检测---verilog---状态机 `timescale 1ns / 1ps /////////////////////////////////////////////////////// ...

  3. ubuntu base make 未找到命令

    引用:https://blog.csdn.net/fenglibing/article/details/7096556 1.先放入UBUNTU安装盘到光盘中: 2.再按顺序执行以下的命令: sudo ...

  4. ie8 报错:意外地调用了方法或属性访问

    在某场景中一句简单的js: $("#changeOption").text("增加"); 在 IE8 下面报错:'意外地调用了方法或属性访问' 改成:$(&qu ...

  5. 胖子哥的大数据之路(11)-我看Intel&&Cloudera的合作

    一.引言 5月8日,作为受邀嘉宾,参加了Intel与Cloudera在北京中国大饭店新闻发布会,两家公司宣布战略合作,该消息成为继Intel宣布放弃大数据平台之后的另外一个热点新闻.对于Intel的放 ...

  6. spring 基本配置学习

    1.bean的方式说明 作用:    用于配置对象让spring来创建的. 默认情况下它调用的是类中的无参构造函数.如果没有无参构造函数则不能创建成功. 属性: id:给对象在容器中提供一个唯一标识. ...

  7. 数字序列中某一位数字(《剑指offer》面试题44)

    由于这道题目在牛客上没有,所以在此记录一下. 一.题目大意: 数字以0123456789101112131415…的格式序列化到一个字符序列中.在这个序列中,第5位(从0开始计数,即从第0位开始)是5 ...

  8. 【Java】Java初始化过程总结

    概述 Java字节代码:byte[] Java类在JVM的表现形式:Class类的对象: Java源代码被编译成class字节码 : Java字节代码 --> Class类的对象: 加载:把Ja ...

  9. HTTP之Cookie

    cookie是什么 浏览器存储在本地电脑上的一小段文本文件,cookie的存在主要是为了解决http协议无状态的问题,例如通过cookie来判断用户的登录状态,是否是某一个用户等. cookie的结构 ...

  10. 峰Redis学习(4)Redis 数据结构(List的操作)

    第四节:Redis 数据结构之List 类型 存储list: ArrayList使用数组方式 LinkedList使用双向链接方式   双向链接表中增加数据 双向链接表中删除数据   存储list常用 ...