Solitaire
Solitaire |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 190 Accepted Submission(s): 68 |
|
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 |
|
Sample Output
YES |
|
Source
Southwestern Europe 2002
|
|
Recommend
Ignatius.L
|
/*
题意:给你四个棋子的坐标,让你判断八步内能不能走到指定位置 初步思路:bfs(),总共四个棋子,每个棋子都有最多四种走法,四个坐标是一种状态,vis八维记录四个棋子的状态 #超内存:什么鬼...找到了,可能是bfs()的时候,爆了 #改进:靠,手残了,判断是不是最后一步的时候,写残了,因为最后四个棋子并不是按照顺序到达四个位置的
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
int step;
int x[],y[];
};//四个棋子的状态
node Start;//初状态
node last;//末状态
bool mapn[][];
bool vis[][][][][][][][];//用于记录四个棋子的八个坐标的状态
int dir[][]={,,-,,,,,-};//方向数组 bool Catch(node a){//判断是不是搜索到最后一步
for(int i=;i<;i++){
if(!mapn[a.x[i]][a.y[i]]) return false;
}
return true;
}
bool judge(node a,int k){//判断要去的那一步有没有棋子
for(int i=;i<;i++){
if(i!=k&&a.x[i]==a.x[k]&&a.y[i]==a.y[k]) return true;
}
return false;
}
bool ok(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[]]==true) return false;
return true;
}
bool bfs(){
queue<node>q;
node Next; vis[Start.x[]][Start.y[]][Start.x[]][Start.y[]]
[Start.x[]][Start.y[]][Start.x[]][Start.y[]]=true; Start.step=; q.push(Start);
while(!q.empty()){
Start=q.front();
q.pop();
// for(int i=0;i<4;i++){
// cout<<Start.x[i]+1<<" "<<Start.y[i]+1<<" ";
// }
// cout<<endl;
if(Start.step>=) return false;
for(int i=;i<;i++){//遍历四个棋子
for(int j=;j<;j++){//遍历四个方向 Next=Start;
Next.x[i]+=dir[j][];
Next.y[i]+=dir[j][];//走向下一步
Next.step++;
if(ok(Next)==false) continue;//下一步违法的 //cout<<"ok"<<endl; if(judge(Next,i)==true){//下一步有棋子了
Next.x[i]+=dir[j][];
Next.y[i]+=dir[j][];//再走一步 if(ok(Next)==false||judge(Next,i)==true) continue;//下一步违法的或者下一步还是有棋子 else{
if(Catch(Next)==true){
// cout<<q.size()<<endl;
return true;
}
vis[Next.x[]][Next.y[]][Next.x[]][Next.y[]]
[Next.x[]][Next.y[]][Next.x[]][Next.y[]]=true; q.push(Next);//放进队列中
}
}else{
if(Catch(Next)==true){
// cout<<q.size()<<endl;
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;
}
void init(){
memset(vis,false,sizeof vis);
memset(mapn,false,sizeof mapn);
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&Start.x[],&Start.y[])!=EOF){
init();
Start.x[]--;
Start.y[]--;
for(int i=;i<;i++){
scanf("%d%d",&Start.x[i],&Start.y[i]);
Start.x[i]--;
Start.y[i]--;
}
//标记初状态 for(int i=;i<;i++){
scanf("%d%d",&last.x[i],&last.y[i]);
last.x[i]--;
last.y[i]--;
mapn[last.x[i]][last.y[i]]=true;
} // for(int i=0;i<4;i++){
// cout<<last.x[i]+1<<" "<<last.y[i]+1<<" ";
// }
// cout<<endl; bool flag=bfs();
printf(flag==true?"YES\n":"NO\n");
}
return ;
}
Solitaire的更多相关文章
- 1455.Solitaire(bfs状态混摇)
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- ruby quiz The Solitaire Cipher
solitaire cipher:http://en.wikipedia.org/wiki/Solitaire_(cipher) https://www.schneier.com/solitaire. ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- uva 10651 - Pebble Solitaire(记忆化搜索)
题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...
- Hdu1401 Solitaire 2017-01-18 17:21 33人阅读 评论(0) 收藏
Solitaire Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
- win10 LTSC系统 安装应用商店和纸牌合集,解决从应用商店安装Solitaire Collection纸牌打开空白的问题
家里台式机换了win10系统,想给老妈玩那个纸牌游戏(我也超喜欢的!. 发现这个系统没有自带纸牌游戏Microsoft Solitaire Collection, 过分的是,连应用商店都没有...呵呵 ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
随机推荐
- angular之$watch方法详解
在$apply方法中提到过脏检查,首先apply方法会触发evel方法,当evel方法解析成功后,会去触发digest方法,digest方法会触发watch方法. (1)$watch简介 在diges ...
- 进入css3动画世界(二)
进入css3动画世界(二) 今天主要来讲transition和transform入门,以后会用这两种属性配合做一些动效. 注:本文面向前端css3动画入门人员,我对这个也了解不深,如本文写的有纰漏请指 ...
- 屏蔽掉Google Chrome 浏览器 textarea 单词拼写检测
可以使用html5的spellcheck属性来关闭对元素内容进行拼写检查. <!-以下两种书写方法正确--> <textarea spellcheck="true" ...
- 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之注册与登录监听
一.引言 在数据库和静态页面都创建好之后,下面就该接着完成后台Node.js监听注册和登录的部分了.这个部分主要使用的技术是:Node.js的Express框架和ajax异步请求.登录和注册的代码实现 ...
- python web框架之Tornado
说Tornado之前分享几个前端不错的网站: -- Bootstrap http://www.bootcss.com/ -- Font Awesome http://fontawesome.io/ - ...
- java web jsp学习笔记--概述-常用语法,指令,动作元素,隐式对象,域对象
JSP学习笔记 1.什么是jsp JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.JSP/Servlet规范.JS ...
- MyBatis Generator配置示例
(一).MBG介绍 MyBatis Generator(MBG)是一个Mybatis的代码生成器,它可以用来生成可以访问(多个)表的基础对象.MBG解决了对数据库操作有最大影响的一些简单的CRUD(插 ...
- JAVA提高四:反射基本应用
在前面一节<http://www.cnblogs.com/pony1223/p/7659210.html>,我们学习了JAVA的反射的相关知识,那么本节我们对前面所学习的知识做一个应用相关 ...
- c#字符编码,System.Text.Encoding类,字符编码大全:如Unicode编码、GB18030、UTF-8,UTF-7,GB2312,ASCII,UTF32,Big5
本页列出来目前window下所有支持的字符编码 ---c#通过 System.Text.Encoding.GetEncodings()获取,里面可以对其进行查询,筛选,对同一个字符,在不同编码进行查 ...
- UWP 手绘视频创作工具技术分享系列 - 位图的绘制
前面我们针对 SVG 的解析和绘制做了介绍,SVG 是图片的一种形式,而另一种很重要的图片是:位图,包括 png.jpeg.bmp 等格式.位图的基本规则是,组成的基本元素是像素点,由宽度 * 高度个 ...
