Code Forces 645A Amity Assessment
A. Amity Assessment
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2 × 2 grid and three tiles labeled ‘A’, ‘B’, and ‘C’. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below:
In order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.
Input
The first two lines of the input consist of a 2 × 2 grid describing the initial configuration of Bessie’s puzzle. The next two lines contain a 2 × 2 grid describing the initial configuration of Elsie’s puzzle. The positions of the tiles are labeled ‘A’, ‘B’, and ‘C’, while the empty cell is labeled ‘X’. It’s guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.
Output
Output “YES”(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print “NO” (without quotes).
Examples
input
AB
XC
XB
AC
output
YES
input
AB
XC
AC
BX
output
NO
我直接暴力来的
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>
#include <map>
using namespace std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int vis[5000];
struct Node
{
int a[2][2];
int s;
int posx;
int posy;
Node(){};
Node(int a[2][2],int s,int posx,int posy)
{
this->s=s;
this->posx=posx;
this->posy=posy;
memcpy(this->a,a,sizeof(this->a));
}
};
Node st,ed;
map<char,int>m;
int kt(int a[2][2])
{
int num=0;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
num=num*10+a[i][j];
return num;
}
int bfs(Node st)
{
queue<Node>q;
q.push(st);
while(!q.empty())
{
Node term=q.front();
q.pop();
if(term.s==ed.s)
{
return 1;
}
for(int i=0;i<4;i++)
{
int xx=term.posx+dir[i][0];
int yy=term.posy+dir[i][1];
if(xx<0||xx>=2||yy<0||yy>=2)
continue;
swap(term.a[term.posx][term.posy],term.a[xx][yy]);
int state=kt(term.a);
if(vis[state])
{
swap(term.a[term.posx][term.posy],term.a[xx][yy]);
continue;
}
vis[state]=1;
q.push(Node(term.a,state,xx,yy));
swap(term.a[term.posx][term.posy],term.a[xx][yy]);
}
}
return 0;
}
int main()
{
m['A']=1;m['B']=2;m['C']=3;m['X']=0;
char b1[2][2];
memset(vis,0,sizeof(vis));
for(int i=0;i<=1;i++)
scanf("%s",b1[i]);
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++)
{
st.a[i][j]=m[b1[i][j]];
if(b1[i][j]=='X')
st.posx=i,st.posy=j;
}
st.s=kt(st.a);
for(int i=0;i<=1;i++)
scanf("%s",b1[i]);
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++)
{
ed.a[i][j]=m[b1[i][j]];
if(b1[i][j]=='X')
ed.posx=i,ed.posy=j;
}
ed.s=kt(ed.a);
if(bfs(st))
printf("YES\n");
else
printf("NO\n");
return 0;
}
Code Forces 645A Amity Assessment的更多相关文章
- CodeForces 645A Amity Assessment
简单模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #incl ...
- Codeforces 645A Amity Assessment【八数码】
题目链接: http://codeforces.com/problemset/problem/645/A 题意: 2*2的八数码问题 分析: 这题n为2,不需要搜索,直接判断字母排列顺序就好了. 注意 ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) A. Amity Assessment 水题
A. Amity Assessment 题目连接: http://www.codeforces.com/contest/655/problem/A Description Bessie the cow ...
- codeforces 655A A. Amity Assessment(水题)
题目链接: A. Amity Assessment time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
随机推荐
- Bootstrap的js插件之折叠(collapse)
data-toggle="collapse"--指明该元素具有折叠功能: data-target--设置元素打开折叠后指向的元素链接. .collapse--用来设置元素为折叠内容 ...
- android.graphics(1) - Paint, Canvas, drawLine, drawPoint, drawRect, drawRoundRect, drawCircle, drawOval, drawArc
一.Paint与Canvas 像我们平时画图一样,需要两个工具,纸和笔.Paint就是相当于笔,而Canvas就是纸,这里叫画布. 所以,凡有跟要要画的东西的设置相关的,比如大小,粗细,画笔颜色,透明 ...
- Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 33554432 bytes
MongoDB执行错误: Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 335 ...
- iOS开发-iOS 10 由于权限问题导致崩溃的那些坑
iOS开发-iOS 10 由于权限问题导致崩溃的那些坑 6月份的WWDC大会结束有一段时间了,相信很多开发者也是在努力工作的闲时用着Xcode8 Beta版学习着新的特性吧. 使用Xcode8写自己 ...
- C++static关键字用法
一.static的作用有三种:限制变量或函数作用域.保持变量内容的持久.默认初始化为0 1.被static关键字修饰的全局函数或者变量具有文件作用域,即只在当前文件中可见. 2.被static修饰的变 ...
- 访问子节点childNodes
访问子节点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...
- vue中axios调用接口和用node.js跨域
<script>const API_PROXY = 'https://bird.ioliu.cn/v1/?url='import axios from 'axios'export defa ...
- 非常多学ThinkPHP的新手会遇到的问题
在模板传递变量的时候,非常多视频教程都使用$v.channel的方式.例如以下: <a href="{:U('Chat/set',array('id'=>$v.channel)) ...
- C语言 · 素数判断
算法提高 素数判断 时间限制:1.0s 内存限制:512.0MB 编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...
- ResourceBundle读取utf-8格式properties 中文乱码
解决方法: ResourceBundle prop = ResourceBundle.getBundle("app");String defaultTunnelName = new ...