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个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
随机推荐
- C#中的两把双刃剑:抽象类和接口
问题出现: 这也是我在学习抽象类和接口的时候遇到的问题,从我归纳的这三个问题,不难看出这也许是我们大多数程序员遇到问题的三个阶段, 第一阶段(基础概念):就象问题1一样,这部分人首先需要扫清基础概念的 ...
- PHP获取文件的绝对路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ===========PH ...
- 学习率 Learning Rate
本文从梯度学习算法的角度中看学习率对于学习算法性能的影响,以及介绍如何调整学习率的一般经验和技巧. 在机器学习中,监督式学习(Supervised Learning)通过定义一个模型,并根据训练集上的 ...
- Quartz源码——Quartz调度器的Misfire处理规则(四)
Quartz调度器的Misfire处理规则 调度器的启动和恢复中使用的misfire机制,还需细化! SimpleTrigger的misfire机制 默认的 Trigger.MISFIRE_INSTR ...
- 程序编译没错,运行报错:无法定位程序输入点GT_BufLaserFollowRatio(这是函数)于动态链接库GTS.DLL上
:DLL里面没有导出该函数 :DLL没放进DEBUGS文件夹 (当时的情况是这个)
- LNMP环境源码搭建
以前LNMP环境是由运维搭建,自己搭建的时候查找了很多资料,这是我见过的最棒的资料,将过程记录下来分享给大家 为啥使用LNMP而不是LAMP下面来谈谈Nginx的技能 Nginx是一个小巧而高效的Li ...
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
drop procedure if exists pro_rep_shadow_rs; delimiter | ---------------------------------- -- rep_sh ...
- LinearGradientBrush,RadialGradientBrush的样式说明
LinearGradientBrush 使用线性渐变绘制区域.线性渐变沿直线定义渐变.该直线的终点由线性渐变的 StartPoint 和 EndPoint 属性定义.LinearGradientBru ...
- C语言通过函数参数不能带出动态内存的例子。
实验结论:通过函数参数不能带出动态内存,函数参数虽然为指针,其实是在函数内部的临时变量,只是该指针的初始值是通过调用函数赋值的.C语言函数参数都是传值的. #include <stdio.h&g ...
- 改变oracle数据库归档模式_译文
Changing the Database Archiving Mode 改变数据库归档模式. Purpose 目的 This module describes how you can change ...
