Solitaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3077    Accepted Submission(s): 954

Problem Description
Solitaire 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.

 
Input
Each 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
The 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.
 
Sample Input
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
 
Sample Output
YES
 
Source
 
Recommend
四个点的哈希,由于这四个点没有什么区分,哈希的时候注意一下。
由于的用set哈希,用*10的方法。所以要先排序。很容易理解的。
bfs的代码,有点意思。
 /**
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6 **/ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<set>
using namespace std; struct node
{
int x[];
int y[];
};
struct node start,end1; queue<node>Q[];
set<int>hxl[];
bool flag;
int map1[][]={{,},{,},{-,},{,-}}; bool fun(node &t,int i)
{
int j;
if(t.x[i]>=&&t.x[i]<= && t.y[i]>=&&t.y[i]<=)
{
for(j=;j<;j++)
{
if(j==i)continue;
if(t.x[i]==t.x[j] && t.y[i]==t.y[j]) return true;
}
return false;
}
return true;
}
void pai(node &t)
{
int i,j,x;
for(i=;i<; i++)
{
x=i;
for(j=i+;j<; j++)
if(t.x[x]>t.x[j])
x=j;
else if(t.x[x]==t.x[j] && t.y[x]>t.y[j])
x=j;
swap(t.x[i],t.x[x]);
swap(t.y[i],t.y[x]);
} }
int serch(node &t)
{
int i,sum=;
for(i=;i<;i++)
sum=sum*+t.x[i]*+t.y[i];
return sum;
}
void bfs(int x)
{
int i,j,size1,k;
node cur,t;
size1=Q[x].size();
while(size1--)
{
cur=Q[x].front();
Q[x].pop();
for(i=;i<;i++)/** every four point **/
{
for(j=;j<;j++) /** n s w e**/
{
t=cur;
t.x[i]=t.x[i]+map1[j][];
t.y[i]=t.y[i]+map1[j][];
if(fun(t,i)==true)
{
t.x[i]=t.x[i]+map1[j][];
t.y[i]=t.y[i]+map1[j][];
if(fun(t,i)==true) continue;
}
pai(t);
k=serch(t);
if(hxl[x].count(k)>)continue;
if(hxl[x^].count(k)>)
{
flag=true;
return;
}
hxl[x].insert(k);
Q[x].push(t);
}
}
}
}
void dbfs()
{
int ans=,k;
pai(start);
k=serch(start);
hxl[].insert(k);
Q[].push(start); pai(end1);
Q[].push(end1);
k=serch(end1);
hxl[].insert(k);
while(true)
{
if(Q[].size()<Q[].size())
bfs();
else bfs();
ans++;
if(ans==)break;
if(flag==true) return;
}
}
int main()
{
int i;
while(scanf("%d%d",&start.x[],&start.y[])>)
{
for(i=;i<;i++)
scanf("%d%d",&start.x[i],&start.y[i]);
for(i=;i<;i++)
scanf("%d%d",&end1.x[i],&end1.y[i]);
while(!Q[].empty()){
Q[].pop();
}
while(!Q[].empty()){
Q[].pop();
}
hxl[].clear();
hxl[].clear();
flag=false;
dbfs();
if(flag==true) printf("YES\n");
else printf("NO\n");
}
return ;
}

hdu 1401的更多相关文章

  1. HDU 1401 Solitaire 双向DFS

    HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...

  2. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  3. hdu 1401(单广各种卡的搜索题||双广秒速)

    Solitaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. POJ 1198/HDU 1401

    双向广搜... 呃,双向广搜一般都都用了HASH判重,这样可以更快判断两个方向是否重叠了.这道题用了双向的BFS,有效地减少了状态.但代码太长了,不写,贴一个别人的代码.. #include<i ...

  5. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

  6. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

  7. hdu 3038 How Many Answers Are Wrong

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...

  8. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  9. HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

随机推荐

  1. openstack 实用命令

    port 1.创建port(create) i.随机ip openstack port create --network public --fixed-ip subnet=sub-public '' ...

  2. 【2019年OCP新题】OCP题库更新出现大量新题-10

    10.Which two statements are true about SQL*Loader Express Mode in an Oracle 12c database? A) It can ...

  3. 用layui遇到过的问题

    1.报错“layui.form is not a function”问题 把代码中这一串修改一下:form = layui.form(); 括号去掉就行: form = layui.form; 如果你 ...

  4. 蒲公英(bzoj2724)(分块+区间众数)

    Input Output Sample Input 6 3 1 2 3 2 1 2 1 5 3 6 1 5 Sample Output 1 2 1 HINT \(n <= 40000\),$ m ...

  5. Python中线程与互斥锁

    了解之前我们先了解一下什么是多任务? 概念: 几个不同的事件在同时运行就是多任务, 这样的话, 我们有牵扯到了真的多任务, 假的多任务; 并行: 真的多任务, 通过电脑的核数来确定 并发: 假的多任务 ...

  6. 怎么在eclipse中访问webservice

    在eclipse创建webservice的方法: 1.在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 P ...

  7. FlexMonkey实战

    文章来源:http://www.cnblogs.com/raol/p/flexmonkey.html 我的有道云笔记:http://note.youdao.com/share/?id=22b79669 ...

  8. php判断是否使用手机访问

    直接上代码 /** * 检测是否使用手机访问 * @access public * @return bool */ public function isMobile() { if (isset($_S ...

  9. 怎样优化调整innodb_log_buffer_size

    官方文档并没有直接告诉如何调整 innodb_log_buffer_size 大小, 根据对mysql 的状态信息了解知道  innodb_log_buffer_size 跟 Innodb_os_lo ...

  10. spring基础回顾

    1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...