hdu 1401(单广各种卡的搜索题||双广秒速)
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4122 Accepted Submission(s): 1245
is a game played on a chessboard 8x8. The rows and columns of the
chessboard are numbered from 1 to 8, from the top to the bottom and from
left to right respectively.
There are four identical pieces on the board. In one move it is allowed to:
> move a piece to an empty neighboring field (up, down, left or right),
> jump over one neighboring piece to an empty field (up, down, left or right).

There
are 4 moves allowed for each piece in the configuration shown above. As
an example let's consider a piece placed in the row 4, column 4. It can
be moved one row up, two rows down, one column left or two columns
right.
Write a program that:
> reads two chessboard configurations from the standard input,
> verifies whether the second one is reachable from the first one in at most 8 moves,
> writes the result to the standard output.
of two input lines contains 8 integers a1, a2, ..., a8 separated by
single spaces and describes one configuration of pieces on the
chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the
position of one piece - the row number and the column number
respectively. Process to the end of file.
output should contain one word for each test case - YES if a
configuration described in the second input line is reachable from the
configuration described in the first input line in at most 8 moves, or
one word NO otherwise.
2 4 3 3 3 6 4 6
#include<stdio.h>
#include<queue>
#include<iostream>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
struct Node{
int x[],y[];
int step;
}s,t;
int graph[][];
bool vis[][][][][][][][];
bool equals(Node a){ ///这个地方开始想错了,以为是每一个点的状态对应下一行每一行的状态,结果是只要能到达目标
///状态就OK,比如说是起始地第二个点到达目标的第一个点
for(int i=;i<;i++){
if(!graph[a.x[i]][a.y[i]]) return false;
}
return true;
}
bool check(Node a){
for(int i=;i<;i++){
if(a.x[i]<||a.x[i]>=||a.y[i]<||a.y[i]>=) return false;
}
if(vis[a.x[]][a.y[]][a.x[]][a.y[]][a.x[]][a.y[]][a.x[]][a.y[]]) return false;
return true;
}
bool cango(Node next,int k){
for(int i=;i<;i++){
if(i!=k&&next.x[i]==next.x[k]&&next.y[i]==next.y[k]) return false;
}
return true;
}
int dir[][] = {{,},{-,},{,},{,-}}; bool bfs(){
queue<Node> q;
q.push(s);
s.step = ;
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.step>){
return false;
}
if(equals(now)){
return true;
}
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.x[i] = now.x[i]+dir[j][];
next.y[i] = now.y[i]+dir[j][];
next.step = now.step+;
if(next.step>) continue; ///大神的剪枝是>=8
if(!check(next)) continue;
if(cango(next,i)){
if(equals(next)) return true;
vis[next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]] = true;
q.push(next);
}
else{
next.x[i] = now.x[i]+*dir[j][];
next.y[i] = now.y[i]+*dir[j][];
if(!check(next)) continue;
if(cango(next,i)){
if(equals(next)) return true;
vis[next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]] = true;
q.push(next);
}
}
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&s.x[],&s.y[])!=EOF){
memset(graph,,sizeof(graph));
s.x[]-=,s.y[]-=;
for(int i=;i<;i++){
scanf("%d%d",&s.x[i],&s.y[i]);
s.x[i]--;
s.y[i]--;
}
for(int i=;i<;i++){
scanf("%d%d",&t.x[i],&t.y[i]);
t.x[i]--;
t.y[i]--;
graph[t.x[i]][t.y[i]] = ;
}
memset(vis,false,sizeof(vis));
vis[s.x[]][s.y[]][s.x[]][s.y[]][s.x[]][s.y[]][s.x[]][s.y[]] = true;
bool flag = bfs();
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
双广的话非常快,主要是怎么处理初始状态和目标状态,因为有可能第1个最后跳到二状态去了,这里的解决办法是排个序之后就解决了。
#include<stdio.h>
#include<queue>
#include<iostream>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
struct Point{
int x,y;
};
struct Node{
Point p[];
int step;
}s,t;
char vis[][][][][][][][];
int graph[][];
int cmp(Point a,Point b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void make_vis(Node s,char c){
vis[s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y] = c;
}
char checkvis(Node s){
return vis[s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y];
}
void make_graph(Node s){
memset(graph,,sizeof(graph));
for(int i=;i<;i++){
graph[s.p[i].x][s.p[i].y] = ;
}
}
bool check(Node s){
for(int i=;i<;i++){
if(s.p[i].x<||s.p[i].x>=) return false;
if(s.p[i].y<||s.p[i].y>=) return false;
}
return true;
}
int dir[][]={{,},{-,},{,},{,-}};
bool bfs(){
memset(vis,,sizeof(vis));
make_vis(s,'');
make_vis(t,'');
queue<Node> q1,q2;
q1.push(s);
q2.push(t);
while(!q1.empty()||!q2.empty()){
if(!q1.empty()){
Node now = q1.front();
q1.pop();
if(now.step>=) continue;
make_graph(now);
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
next.step++;
if(!check(next)) continue;
if(graph[next.p[i].x][next.p[i].y]){
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
if(!check(next)||graph[next.p[i].x][next.p[i].y]) continue;
}
sort(next.p,next.p+,cmp);
if(checkvis(next)=='') continue;
else if(checkvis(next)=='') return true;
make_vis(next,'');
q1.push(next);
}
}
}
if(!q2.empty()){
Node now = q2.front();
q2.pop();
if(now.step>=) continue;
make_graph(now);
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
next.step++;
if(!check(next)) continue;
if(graph[next.p[i].x][next.p[i].y]){
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
if(!check(next)||graph[next.p[i].x][next.p[i].y]) continue;
}
sort(next.p,next.p+,cmp);
if(checkvis(next)=='') continue;
else if(checkvis(next)=='') return true;
make_vis(next,'');
q2.push(next);
}
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&s.p[].x,&s.p[].y)!=EOF){
memset(graph,,sizeof(graph));
s.p[].x-=,s.p[].y-=;
for(int i=;i<;i++){
scanf("%d%d",&s.p[i].x,&s.p[i].y);
s.p[i].x--;
s.p[i].y--;
}
for(int i=;i<;i++){
scanf("%d%d",&t.p[i].x,&t.p[i].y);
t.p[i].x--;
t.p[i].y--;
}
s.step = t.step = ;
sort(s.p,s.p+,cmp);
sort(t.p,t.p+,cmp);
bool flag = bfs();
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
hdu 1401(单广各种卡的搜索题||双广秒速)的更多相关文章
- POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)
题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
- hdu.1043.Eight (打表 || 双广 + 奇偶逆序)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- poj 1416 (hdu 1539)Shredding Company:剪枝搜索
点击打开链接 题目大意是有一个分割机,可以把一串数字分割成若干个数字之后求和,题目输入一个数字上界和待分割的数字,让我们求出分割后数字之和在不超过给定max的情况下的最大值,并且给出分割方案,如果没有 ...
- 历年NOIP中的搜索题
什么题目都不会做于是开始做搜索题. 然而我搜索题也不会做了. 铁定没戏的蒟蒻. 1.NOIP2004 虫食算 “对于给定的N进制加法算式,求出N个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...
随机推荐
- 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic
本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...
- Mycat高可用解决方案二(主从复制)
Mycat高可用解决方案二(主从复制) 系统部署规划 名称 IP 主机名称 用户名/密码 配置 mysql主节点 192.168.199.110 mysql-01 root/hadoop 2核/2G ...
- Pyhon从入门到致命
第一章 基础 1.python2和python3的区别 2.数据类型 2.1 int 整型 2.2 str 字符串不可变类型 2.3 bool 布尔类型 2.4 list 列表 2.5 tuple 元 ...
- GoF23种设计模式之行为型模式之中介者模式
一.概述 使用一个中介对象来封装一系列的对象交互.中介者让各个对象无需显式地相互引用,从而达到解耦的效果.并且可以独立地改变它们之间的交互.二.适用性1.当一组对象以定义良好但复杂通信的时候.产生的相 ...
- 让Python带你看一场唯美的横飘雪!
“北国风光,千里冰封,万里雪飘”,这句诗描写了一句美丽肃静的风光图,恰逢昨天笔者这边也下了一场比较大的雪,要不今天就用Python带大家也来领略一次美丽的雪景? 开发环境 版本:Python3.6 系 ...
- hdu 5533
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- poj 1321 排兵布阵问题 dfs算法
题意:有不规则地图,在上面放n个相同的棋子,要求摆放的时候不同行不同列.问:有多少种摆法? 思路:dfs+回溯 用一个book[]数组来表示当前列是否有放棋子 一行一行的遍历,对一行来说遍历它的列,如 ...
- list_for_each_entry()函数分析
list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...
- PAT Basic 1080
1080 MOOC期终成绩 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分 ...
- Selenium2用最简xpath查找元素
什么是xpath? 来自百度百科的解释:XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言.XPath基于XML的树状结构,提供在数据结构树中找寻节点 ...