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个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
随机推荐
- Python 接口测试(二)
三:http状态码含义(来源于w3school): 状态码: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客 ...
- oracle pl/sql 基础
一.pl/sql developer开发工具pl/sql developer是用于开发pl/sql块的集成开发环境(ide),它是一个独立的产品,而不是oracle的一个附带品. 二.pl/sql介绍 ...
- React——高阶组件
1.在React中higher-order component (HOC)是一种重用组件逻辑的高级技术.HOC不是React API中的一部分.HOC是一个函数,该函数接收一个组件并且返回一个新组件. ...
- 【转】常用Maven插件
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- Centos7 创建本地 docker 仓库极其遇到的问题
环境安装: VirtualBox 安装 Centos7 安装 docker 1. 配置私有仓库和客户端地址 私有仓库:192.168.1.104 客户端:192.168.1.103 通过 Centos ...
- Mac上搭建基于Github的Hexo博客
Mac 上搭建基于Github的hexo博客 博客地址:往事亦如风的博客 hexo官方文档 本来想搭一个自己的博客,但是因为服务器真心买不起,所以就使用gitpages搭建一个免费的博客. 环境配置 ...
- ch4-计算属性(表达式计算 computed methods watchers)
1 计算属性 1.1 模板内的表达式是非常便利的,但是它们实际上只用于简单的运算. 在模板中放入太多的逻辑会让模板过重且难以维护. <div id="test1"> { ...
- ThinkJS框架入门详细教程(二)新手入门项目
一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...
- Jquery EasyUI Base基础
<pre><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- Java高新技术 JavaBean内省
Java高新技术 JavaBean内省 知识概要: (1)了解JavaBean内省 (2)JavaBean的简单内省操作 ...
