Hdu 6171 Admiral【双向Bfs+字符串哈希】
来源:互联网 发布:西门子数控编程软件 编辑:程序博客网 时间:2024/05/21 06:31
Admiral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 203 Accepted Submission(s): 52
Problem Description
Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.
Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.
Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.
Input
The first line of input contains an integer T (1 <= T <= 10), the number of test cases.
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.
Output
For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.
Sample Input
112 02 1 23 3 3 34 4 4 4 45 5 5 5 5 5
Sample Output
3
题目大意:
现在给出一个三角矩阵,我们如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换。
我们每一次只能对0点和其他点进行交换。问最少步数,使得最终变成:
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
思路:
双向Bfs,我们正向预处理10步,然后根据读入的数据再走十步,当我们根据读入的数据走的时候,如果能够遇到之前预处理走到的点的话,那么ans=steppre+stepnow;
维护最小即可。
考虑到每个点的状态有6个,那么对应6^21的vis数组我们是开不出来的,所以我们需要字符串哈希一下判定一个点是否走过即可。
Ac代码:
#include<stdio.h>#include<iostream>#include<string.h>#include<map>#include<queue>using namespace std;map<unsigned long long int,int>ss,ss2;unsigned long long int h[45];struct node{ int x,y,step; char temp[25];}now,nex;int a[8][8];int output;int poss[8][8];int fx[4]={1,-1,1,-1};int fy[4]={0,0,1,-1};int getpos(int x,int y){ return poss[x][y];}unsigned long long int Getval(char s[]){ int n=strlen(s); unsigned long long int x=1; for(int i=0;i<n;i++) { h[i]=h[i-1]+x*(s[i]-'0'); x=x*19260817; } return h[n-1];}void Bfs(){ queue<node>s; now.x=1; now.y=1; now.step=0; int cnt=0; for(int i=1;i<=6;i++) { for(int j=1;j<=i;j++) { poss[i][j]=cnt; now.temp[cnt++]=i+'0'-1; } } now.temp[cnt]='\0'; s.push(now); while(!s.empty()) { now=s.front();s.pop(); ss[Getval(now.temp)]=now.step; if(now.step>=10)break; for(int i=0;i<4;i++) { nex=now; nex.x+=fx[i]; nex.y+=fy[i]; nex.step++; if(nex.x>=1&&nex.x<=6&&nex.y>=1&&nex.y<=nex.x) { int pos1=getpos(now.x,now.y); int pos2=getpos(nex.x,nex.y); swap(nex.temp[pos1],nex.temp[pos2]); unsigned long long int nexval=Getval(nex.temp); if(ss[nexval]==0) { ss[nexval]=nex.step; s.push(nex); } } } }}void Bfs2(){ ss2.clear(); queue<node>s; output=0x3f3f3f3f; int cnt=0; for(int i=1;i<=6;i++) { for(int j=1;j<=i;j++) { now.temp[cnt++]=a[i][j]+'0'; if(a[i][j]==0) { now.x=i; now.y=j; now.step=0; } } } now.temp[cnt]='\0'; s.push(now); while(!s.empty()) { now=s.front();s.pop(); ss2[Getval(now.temp)]=now.step; if(now.step>=10)break; for(int i=0;i<4;i++) { nex=now; nex.x+=fx[i]; nex.y+=fy[i]; nex.step++; if(nex.x>=1&&nex.x<=6&&nex.y>=1&&nex.y<=nex.x) { int pos1=getpos(now.x,now.y); int pos2=getpos(nex.x,nex.y); swap(nex.temp[pos1],nex.temp[pos2]); unsigned long long int nexval=Getval(nex.temp); if(ss2[nexval]==0) { ss2[nexval]=nex.step; s.push(nex); } if(ss[nexval]>0) { output=min(output,nex.step+ss[nexval]); } } } } if(output==0x3f3f3f3f)printf("too difficult\n"); else printf("%d\n",output);}int main(){ ss.clear(); Bfs(); int t; scanf("%d",&t); while(t--) { int len=0; char bb[55]; char aa[55]; for(int i=1;i<=6;i++) { for(int j=1;j<=i;j++) { bb[len]=i-1+'0'; scanf("%d",&a[i][j]); aa[len++]=a[i][j]+'0'; } } bb[len]='\0'; aa[len]='\0'; if(strcmp(aa,bb)==0) { printf("0\n"); continue; } int anssss=ss[Getval(aa)]; if(anssss==0) { Bfs2(); } else printf("%d\n",anssss); }}
阅读全文
0 0
- Hdu 6171 Admiral【双向Bfs+字符串哈希】
- HDU 6171 Admiral [双向bfs+hash]
- hdu 6171 Admiral 双向bfs+hash
- 【多校训练】hdu 6171 Admiral 双向bfs+hash
- 2017多校联合第十场/HDU 6171 Admiral 双向bfs + 哈希
- HDU 6171 Admiral(双向宽搜)
- HDU 6171 Admiral
- HDU 6171 Admiral 双向搜索(meet in the middle) + 哈希
- 2017 Multi-University Training Contest 10 1001 Admiral HDU 6171 (双向搜索 哈希)
- HDU 6171 Admiral (DFS + 剪枝)
- Admiral HDU
- hdu 1401 双向bfs
- hdu 3085 双向BFS
- hdu 3085(双向bfs)
- hdu 1195 双向bfs
- HDU 1560 双向BFS
- HDU 3085 双向BFS
- HDU 1401 双向BFS !!!
- Android 内存查看常用命令
- springmvc验证登录用过滤器还是拦截器
- HDU 2209 翻纸牌游戏
- 【JZOJ5335】【NOIP2017提高A组模拟8.24】早苗
- LICEcap 屏幕录制工具
- Hdu 6171 Admiral【双向Bfs+字符串哈希】
- Runtime的总结和应用
- 表达式树---后缀表达式转中缀表达式
- IDEA(JAVA)使用json
- 判断一棵树是否是二叉查找树
- stm32+pca9685控制舵机机械臂
- 三个触发器解决变异表错误(ORA-04091)
- MyBatis的简单使用
- Java 排序算法-归并排序