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. FreePascal - CodeTyphon交叉编译,在一个操作系统生成各个操作系统可以运行的程序!

    致谢:[XE3]MN,让我加快完成了使用CodeTyphon进行交叉编译! CodeTyphon版本: 6.0 下载:http://www.pilotlogic.com/codetyphon/zips ...

  2. Weekly Contest 130

    1029. Binary Prefix Divisible By 5 Given an array A of 0s and 1s, consider N_i: the i-th subarray fr ...

  3. Atcoder Tenka1 Programmer Contest 2019题解

    传送门 \(C\ Stones\) 最后肯定形如左边一段白+右边一段黑,枚举一下中间的断点,预处理一下前缀和就可以了 int main(){ // freopen("testdata.in& ...

  4. 约瑟夫(Josephus)问题~转

    本文都是转的,一个是转博客,一个是转贴吧,前者详细,后者"强,无敌"! 博客转: 以前就知道约瑟夫问题是模拟,今天我才发现一些约瑟夫问题可以使用数学解法得出!真是强悍啊!约瑟夫问题 ...

  5. 【wireshark】插件开发(四):Lua插件Post-dissector和Listener

    1. Post-dissector post-dissector和dissector不同,它会在所有dissectors都执行过后再被执行,这也就post前缀的由来.post-dissector的构建 ...

  6. day 47 Django 4的简单应用 创建简单的图书管理 (单表的增删改查)

    前情提要  Django  已经学了大半.. 很多东西已经能够使用在生产环境当中 一:模糊查询 二:单表删除 三:单表修改 四:图书管理 图书管理操作 视图结构 A:路由层 A :配置路由文件 参数解 ...

  7. JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)

    JIRA是一个缺陷跟踪管理系统,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域,当我们需要把第三方业务系统集成进来时,可以调用他的API. JIRA本身的A ...

  8. 深入理解Java接口和抽象类

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...

  9. 剑指offer二十一之栈的压入、弹出序列

    一.题目 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...

  10. 渐进增强与优雅降级 && css3中普通属性和前缀属性的书写顺序

     什么是渐进增强与优雅降级? 服务器和浏览器是不同的.当服务器有新版本时,开发人员直接使用新版本的服务器提供服务即可:但是浏览器端,不同的用户使用的浏览器版本不同,型号差异大,我们不可能让用户强制更新 ...