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个不同的字母分别代表的数字,使得该加法算式成立.输入数据 ...
随机推荐
- php curl使用例子
PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯.libcurl目前支持http.https.ftp.gopher.telnet. ...
- Golang ioutil读写文件测试
运用 ioutil.ReadFile .ioutil.WriteFile package main import ( "io/ioutil" "log" &qu ...
- 02等待单个线程返回WaitForSingleObject
windows 多线程之等待线程返回 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...
- 如何使用postman做接口测试
1.get请求传参 只要是get请求都可以在浏览器中直接发: 在访问地址后面拼 ?key=value&key=value 例如: 在浏览器中直接输入访问地址,后面直接拼需要传给服务器的参数 ...
- Python分布式爬虫开发搜索引擎 Scrapy实战视频教程
点击了解更多Python课程>>> Python分布式爬虫开发搜索引擎 Scrapy实战视频教程 课程目录 |--第01集 教程推介 98.23MB |--第02集 windows下 ...
- MySQL创建根据经纬度计算距离的函数
按照经纬度计算距离 日常开发中,特别是做微信项目时,经常会遇到根据用户地理位置来展示附近商家的功能,通常解决这种问题的思路是,后台设置商家的经纬度,然后再根据前台传的经纬度进行计算,具体经纬度转换以及 ...
- Manjaro 添加国内源和安装搜狗输入法
Manjaro 系统虽然比 Ubuntu 用着稳定,但有些小地方没有 Ubuntu 人性化,比如默认安装完的系统貌似没有中国的,Ubuntu 估计是用的人多,所以安装完后会根据所在地给你配置更新的源. ...
- manjaro18 配置国内镜像源
1.配置镜像源: sudo pacman-mirrors -i -c China -m rank 2.设置 archlinuxcn 源: sudo nano /etc/pacman.conf 添加以下 ...
- 剑指Offer(书):不用四则运算做加法
题目:写一个函数,求两个整数之和,不得使用四则运算位运算. package com.gjjun.jzoffer; /** * 写一个函数,求两个整数之和,不得使用四则运算 * * @author gj ...
- PTA 7-1 银行业务队列简单模拟
用链表实现队列操作,代码如下: #include <iostream> #include <cstdio> #include <algorithm> #includ ...