hdu 1401(单广各种卡的搜索题||双广秒速)
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4122 Accepted Submission(s): 1245
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.
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 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.
2 4 3 3 3 6 4 6
#include<stdio.h>
#include<queue>
#include<iostream>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
struct Node{
int x[],y[];
int step;
}s,t;
int graph[][];
bool vis[][][][][][][][];
bool equals(Node a){ ///这个地方开始想错了,以为是每一个点的状态对应下一行每一行的状态,结果是只要能到达目标
///状态就OK,比如说是起始地第二个点到达目标的第一个点
for(int i=;i<;i++){
if(!graph[a.x[i]][a.y[i]]) return false;
}
return true;
}
bool check(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[]]) return false;
return true;
}
bool cango(Node next,int k){
for(int i=;i<;i++){
if(i!=k&&next.x[i]==next.x[k]&&next.y[i]==next.y[k]) return false;
}
return true;
}
int dir[][] = {{,},{-,},{,},{,-}}; bool bfs(){
queue<Node> q;
q.push(s);
s.step = ;
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.step>){
return false;
}
if(equals(now)){
return true;
}
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.x[i] = now.x[i]+dir[j][];
next.y[i] = now.y[i]+dir[j][];
next.step = now.step+;
if(next.step>) continue; ///大神的剪枝是>=8
if(!check(next)) continue;
if(cango(next,i)){
if(equals(next)) return true;
vis[next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]][next.x[]][next.y[]] = true;
q.push(next);
}
else{
next.x[i] = now.x[i]+*dir[j][];
next.y[i] = now.y[i]+*dir[j][];
if(!check(next)) continue;
if(cango(next,i)){
if(equals(next)) 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;
}
int main()
{
while(scanf("%d%d",&s.x[],&s.y[])!=EOF){
memset(graph,,sizeof(graph));
s.x[]-=,s.y[]-=;
for(int i=;i<;i++){
scanf("%d%d",&s.x[i],&s.y[i]);
s.x[i]--;
s.y[i]--;
}
for(int i=;i<;i++){
scanf("%d%d",&t.x[i],&t.y[i]);
t.x[i]--;
t.y[i]--;
graph[t.x[i]][t.y[i]] = ;
}
memset(vis,false,sizeof(vis));
vis[s.x[]][s.y[]][s.x[]][s.y[]][s.x[]][s.y[]][s.x[]][s.y[]] = true;
bool flag = bfs();
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
双广的话非常快,主要是怎么处理初始状态和目标状态,因为有可能第1个最后跳到二状态去了,这里的解决办法是排个序之后就解决了。
#include<stdio.h>
#include<queue>
#include<iostream>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
struct Point{
int x,y;
};
struct Node{
Point p[];
int step;
}s,t;
char vis[][][][][][][][];
int graph[][];
int cmp(Point a,Point b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void make_vis(Node s,char c){
vis[s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y] = c;
}
char checkvis(Node s){
return vis[s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y][s.p[].x][s.p[].y];
}
void make_graph(Node s){
memset(graph,,sizeof(graph));
for(int i=;i<;i++){
graph[s.p[i].x][s.p[i].y] = ;
}
}
bool check(Node s){
for(int i=;i<;i++){
if(s.p[i].x<||s.p[i].x>=) return false;
if(s.p[i].y<||s.p[i].y>=) return false;
}
return true;
}
int dir[][]={{,},{-,},{,},{,-}};
bool bfs(){
memset(vis,,sizeof(vis));
make_vis(s,'');
make_vis(t,'');
queue<Node> q1,q2;
q1.push(s);
q2.push(t);
while(!q1.empty()||!q2.empty()){
if(!q1.empty()){
Node now = q1.front();
q1.pop();
if(now.step>=) continue;
make_graph(now);
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
next.step++;
if(!check(next)) continue;
if(graph[next.p[i].x][next.p[i].y]){
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
if(!check(next)||graph[next.p[i].x][next.p[i].y]) continue;
}
sort(next.p,next.p+,cmp);
if(checkvis(next)=='') continue;
else if(checkvis(next)=='') return true;
make_vis(next,'');
q1.push(next);
}
}
}
if(!q2.empty()){
Node now = q2.front();
q2.pop();
if(now.step>=) continue;
make_graph(now);
for(int i=;i<;i++){
for(int j=;j<;j++){
Node next = now;
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
next.step++;
if(!check(next)) continue;
if(graph[next.p[i].x][next.p[i].y]){
next.p[i].x += dir[j][];
next.p[i].y += dir[j][];
if(!check(next)||graph[next.p[i].x][next.p[i].y]) continue;
}
sort(next.p,next.p+,cmp);
if(checkvis(next)=='') continue;
else if(checkvis(next)=='') return true;
make_vis(next,'');
q2.push(next);
}
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&s.p[].x,&s.p[].y)!=EOF){
memset(graph,,sizeof(graph));
s.p[].x-=,s.p[].y-=;
for(int i=;i<;i++){
scanf("%d%d",&s.p[i].x,&s.p[i].y);
s.p[i].x--;
s.p[i].y--;
}
for(int i=;i<;i++){
scanf("%d%d",&t.p[i].x,&t.p[i].y);
t.p[i].x--;
t.p[i].y--;
}
s.step = t.step = ;
sort(s.p,s.p+,cmp);
sort(t.p,t.p+,cmp);
bool flag = bfs();
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
hdu 1401(单广各种卡的搜索题||双广秒速)的更多相关文章
- POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)
题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
- hdu.1043.Eight (打表 || 双广 + 奇偶逆序)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- poj 1416 (hdu 1539)Shredding Company:剪枝搜索
点击打开链接 题目大意是有一个分割机,可以把一串数字分割成若干个数字之后求和,题目输入一个数字上界和待分割的数字,让我们求出分割后数字之和在不超过给定max的情况下的最大值,并且给出分割方案,如果没有 ...
- 历年NOIP中的搜索题
什么题目都不会做于是开始做搜索题. 然而我搜索题也不会做了. 铁定没戏的蒟蒻. 1.NOIP2004 虫食算 “对于给定的N进制加法算式,求出N个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...
随机推荐
- C++ 学习笔记(一) cout 与printf 的不同之处
作为一个嵌入式开发的猿,使用打印调试程序是必不可少的,拿到新的项目第一件事就是捣鼓打印.这次也不例外有打印才有耍下去的底气.在之前零零碎碎的C++学习中,还是一边学一边做项目的状态下能用printf解 ...
- jenkins 全局工具配置
- 再生龙备份还原linux系统
相关下载: Clonezilla再生龙:http://sourceforge.net/projects/clonezilla/files/clonezilla_live_stable/ tuxboot ...
- 使用jmeter做简单的压测(检查点、负载设置、聚合报告)
1.添加断言(检查点) 在需要压测的接口下添加--断言--响应断言,取接口响应中包含有的数据即可 检查点HTTP请求-->断言-->响应断言1.名称.注释2.Apply to//作用于哪里 ...
- 动态设置html的title
使用vue前端框架做,竟然丢弃了很多javascript和html的东西了..动态设置title的方法: 1.使用vue的自定义指令 <div v-title>{{htmltitle}}& ...
- Nastya Studies Informatics CodeForces - 992B (大整数)
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- 关于C#Debug和Release
在程序调试时的debug和release 网上有如下的描述:Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进行了各种优化,使 ...
- winServer08上安装SQL时提示“必须使用管理角色安装”或配置microsoft.net framework 3.5
server 2008安装vs2008后报错,如图: 解决方法: 控制面板—>程序—>打开或关闭Windows功能—>进入服务器管理器选择功能—>添加功能 然后勾选.NET F ...
- 如何打造一个"逼格"的web前端项目
最近利用空余的时间(坐公交车看教程视频),重新了解了前后端分离,前端工程化等概念学习,思考如何打造一个“逼格”的web前端项目. 前端准备篇 前端代码规范:制定前端开发代码规范文档. PS:重中之中, ...
- 聊聊、Nginx GDB与MAIN参数
接着上一篇,我们学习 Nginx 的 main 方法.用 gdb 工具调试 Nginx,首先 gdb nginx.如下: gdb 调试工具有很多的命令,上一篇为了找 main 方法用了 b 命令,也就 ...