hdu6171 Admiral 双向宽搜
来源:互联网 发布:淘宝接口进销存软件 编辑:程序博客网 时间:2024/06/07 05:13
Admiral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 82 Accepted Submission(s): 19
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
Source
2017 Multi-University Training Contest - Team 10
#include<stdio.h>#include<algorithm>#include<string.h>#include<math.h>#include<queue>#include<map>#define N 20002using namespace std;typedef long long ll;const int M=1e15+7;struct node{ int x,y,d,f,a[6][6]; ll h;};map<ll,int>mp;ll ha[25];int u[]={-1,-1,1,1};int v[]={-1,0,0,1};ll get_hash(node e)//康托展开,压缩空间,根据逆序对的个数{ int b[25],i,j,k=0; ll ans=0; for(int i=0;i<6;i++){ for(int j=0;j<=i;j++) b[k++]=e.a[i][j]; } for(i=0;i<21;i++) { k=0; for(j=0;j<i;j++) if(b[j]>b[i])k++; (ans+=ha[i]*k%M)%=M; } return ans;}int dfs(node s,node to){ queue<node>Q; mp.clear(); s.h=get_hash(s); to.h=get_hash(to); mp[s.h]=1; mp[0]=-1; Q.push(s); Q.push(to); while(!Q.empty()) { node p=Q.front(); Q.pop(); if(mp[p.h]*p.f<0)return fabs(mp[p.h]-p.d*p.f)-2; for(int i=0;i<4;i++) { node now=p; now.x+=u[i]; now.y+=v[i]; if(now.x<0||now.x>5||now.y<0||now.y>now.x)continue; swap(now.a[p.x][p.y],now.a[now.x][now.y]); now.h=get_hash(now); now.d++; if(mp[now.h]*now.f<0)return fabs(mp[now.h]-now.d*now.f)-2; if(mp[now.h]*now.f>0)continue; mp[now.h]=now.d*now.f; if(now.d<11)Q.push(now); } } return -1;}int main (){ int t; ha[1]=1; for(int i=2;i<=22;i++) ha[i]=ha[i-1]*i%M; for(scanf("%d",&t);t;t--) { node s,to; for(int i=0;i<6;i++){ for(int j=0;j<=i;j++){ scanf("%d",&s.a[i][j]); if(!s.a[i][j])s.x=i,s.y=j; to.a[i][j]=i; } } s.d=1;to.d=1; s.f=1;to.f=-1; to.x=0;to.y=0; int ans=dfs(s,to); if(ans==-1)printf("too difficult\n"); else printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu6171 Admiral 双向宽搜
- HDU 6171 Admiral(双向宽搜)
- hdu6171 双向bfs
- hdu6171 双向搜索+hash
- hdu6171-(双向bfs-hash表)
- HDU 6171 Admiral [双向bfs+hash]
- hdu 6171 Admiral 双向bfs+hash
- Hdu 6171 Admiral【双向Bfs+字符串哈希】
- 【多校训练】hdu 6171 Admiral 双向bfs+hash
- HDU 6171 Admiral 双向搜索(meet in the middle) + 哈希
- 2017多校联合第十场/HDU 6171 Admiral 双向bfs + 哈希
- 2017 Multi-University Training Contest 10 1001 Admiral HDU 6171 (双向搜索 哈希)
- UVa1658 Admiral
- Admiral UVA
- Admiral HDU
- Admiral UVA
- LA6266 Admiral 费用流
- UVa 1658 Admiral
- 看图理解JWT如何用于单点登录
- java equals的用法 以及 equals的实现
- 12.css可见性、内容移出
- Zabbix搭建笔记[9]--报警设置示例
- Springmvc笔记(一)
- hdu6171 Admiral 双向宽搜
- centos7 下时间同步问题
- Erlang 简易安装和卸载
- 数字组合(背包计数)
- 7-8 字符串转换成十进制整数
- 两种适配器模式
- ubuntu下安装nginx时依赖库zlib,pcre,openssl安装方法
- 朝花夕拾,亦不算晚
- zabbix监控mysql最简单的方法