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个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
随机推荐
- POJ3069(贪心+巧用优先队列)
题目传送门:http://poj.org/problem?id=3069 题目大意:一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于每个点,与其距离为R的范围内必有做标记 ...
- MX4拍摄视频转码方法
问题 使用魅族4手机拍摄的视频,其视频编码是H.265 目前大多数设备不支持解码,表现为常用播放器无法正常播放视频,剪辑软件无法剪辑视频. 解决方案 使用软件进行转码,期间尝试软件如下: 爱剪辑 部分 ...
- oracle sql*plus常用命令
一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户 ...
- SpringMVC学习笔记(二)
一.导航 复杂类型的参数绑定 校验 异常处理 图片上传 json交互 拦截器 二.复杂类型参数绑定 ①.包装类型的pojo参数绑定 使用场景:实现商品查询条件传入. 实现方法:>通过添加Http ...
- Max Sum of Max-K-sub-sequence hdu3415
Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- K相邻算法
刚开始学习机器学习,先跟这<机器学习实战>学一些基本的算法 ----------------------------------分割线--------------------------- ...
- FPGA在电平接口领域的应用
电子技术的发展,产生了各种各样的电平接口. TTL电平: TTL电平信号之所以被广泛使用,原因是因为:通常我们采用二进制来表示数据.而且规定,+5V等价于逻辑"1",0V等价于逻辑 ...
- Python自学笔记-进程,线程(Mr serven)
对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程,打开一个Word就启动了 ...
- python读取命令行参数的方法
1.sys模块 需要模块:sys参数个数:len(sys.argv)脚本名: sys.argv[0]参数1: sys.argv[1]参数2: sys.argv[2] test.p ...
- 个人工作中ssd、audio python脚本总结
1.os.system(cmd)或者os.popen(cmd)调用外部命令 cmd中需要注意特殊字符的转义功能,如: USBSTOR\DISK&VEN_GENERIC-&PROD_SD ...