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. hdu2829 Lawrence

    题目链接:戳我 朴素DP:\(dp[i][j]=dp[i-1][k]+cost[k+1][j]\) 其中dp[i][j]表示炸第i次的时候,处理到前j个的最小值是多少.cost[i][j]表示的是i, ...

  2. python scapy 网卡发包

    from scapy.all import * pkt = Ether(src='11:22:33:44:55:77', dst='11:22:33:44:55:66')/ARP(op="w ...

  3. Redis的快照

    博客链接:http://www.cnblogs.com/zhenghongxin/p/8669913.html redis 本地持久化到硬盘有两种方式,一是快照(snapshotting),二是只追加 ...

  4. python学习笔记6-集合

    # 集合是无序且不可重复的元素的集合 a = set([1,3,1,3,3,2,2,5]) a # {1, 2, 3, 5} b = set(range(2,5)) b # {2, 3, 4} # 1 ...

  5. 服务器 apache配置https,http强制跳转https(搭建http与https共存)

    公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...

  6. FunDA(14)- 示范:并行运算,并行数据库读取 - parallel data loading

    FunDA的并行数据库读取功能是指在多个线程中同时对多个独立的数据源进行读取.这些独立的数据源可以是在不同服务器上的数据库表,又或者把一个数据库表分成几个独立部分形成的独立数据源.当然,并行读取的最终 ...

  7. Android应用安全防护和逆向分析 ——apk反编译

    概述 最近一直在学习Android应用安全相关和逆向分析的知识.现在移动app在安全方面是越来越重视了,特别是那些巨头企业涉及到钱的应用,那加密程度,简直是丧心病狂,密密麻麻.从这里可以看出,对于应用 ...

  8. win10无法访问samba共享

    地址: https://blog.csdn.net/xiaohuixing16134/article/details/79601064?utm_source=blogxgwz1 问题描述:配置好sam ...

  9. 项目实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错,eclipse中配置lombok

    @Data注解来源与Lombok,可以减少代码中大量的set get方法,大量减少冗余代码,但是今天部署项目时候,发现实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错. ...

  10. JS: 数据结构与算法之栈

    栈 先来看一道题 Leetcode 32 Longest Valid Parentheses (最长有效括号) 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 ...