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. Day 32 网络编程

    一.网络协议基础篇 一台完整的计算机由硬件.系统.软件组成,具备这三个条件,计算机就可以运行,但是只能自己和自己玩.为了实现计算机和计算机间的连接,就需要借助互联网,如全世界人类交流将英语作为标准语言 ...

  2. Day 12 装饰器,开发封闭.

    一.什么是装饰器 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事务 ...

  3. redis 数据备份持久化方案

    本文链接:http://www.cnblogs.com/zhenghongxin/p/9050219.html 使用两种备份方案 备份方案选择RDB和AOF同时进行备份,必须打开AOF的持久化机制,除 ...

  4. python爬虫2——下载文件(中华网图片库下载)

    # -*- coding: utf-8 -*- import requests import re import sys reload(sys) sys.setdefaultencoding('utf ...

  5. centos 安装oracle 11g r2(二)-----监听配置与创建数据库实例

    centos 安装oracle 11g r2(二)-----监听配置与创建数据库实例 一.监听配置(命令:netca) 1.以 oracle 用户输入命令,启动图形化工具配置监听 [oracle@lo ...

  6. 非常好用的@ResponseBody注解

    AJAX的写法: ajax接收json格式: ①如果ajax接收的是text dataType:"text", var json = eval("(" + da ...

  7. 无apk,怎么获取app的activity

    在做app自动化测试之前,有个前提条件,就是要获取当前app的包名和当前活动的activity.如果有提供了.apk,就可以直接通过adb命令获取到包名和欢迎页面:有种软件是手机自带的的,我们不知道a ...

  8. linux下mysql主从复制搭建

    目标:搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,实现主从复制 环境: 主数据库: 192.168.1.1 从数据库: 192.168.1.2 mysql安装可参考:https:// ...

  9. vba调用c#dll

    本文阐述如何用C#创建COM组件,并能用VB6.0等调用.附有完整测试通过的代码.该功能总体看来很简单,实际值得注意的地方还是挺多.因为很少有人写这类文章,有些代码也是转来转去的不全,有些甚至让人误入 ...

  10. (转)MySQL数据丢失讨论

    原文地址:http://hatemysql.com/tag/sync_binlog/ 1.  概述 很多企业选择MySQL都会担心它的数据丢失问题,从而选择Oracle,但是其实并不十分清楚什么情况下 ...