题目描述
FJ has installed a beautiful pond for his cows' aesthetic enjoyment and exercise.
The rectangular pond has been partitioned into square cells of M rows and N columns (1 ≤ M ≤ 30; 1 ≤ N ≤ 30). Some of the cells have astonishingly sturdy lilypads; others have rocks; the remainder are just beautiful, cool, blue water.
Bessie is practicing her ballet moves by jumping from one lilypad to another and is currently located at one of the lilypads. She wants to travel to another lilypad in the pond by jumping from one lilypad to another.
Surprising only to the uninitiated, Bessie's jumps between lilypads always appear as a chess-knight's move: one move in one direction and then two more in the orthogonal direction (or perhaps two in one direction and then one in the orthogonal direction).
Farmer John is observing Bessie's ballet drill and realizes that sometimes she might not be able to jump to her destination lilypad because intermediary lilypads are missing.
Ever thrifty, he wants to place additional lilypads so she can complete her quest (perhaps quickly, perhaps by using a large number of intermediate lilypads). Of course, lilypads cannot be placed where rocks already intrude on a cell.
Help Farmer John determine the minimum number of additional lilypads he has to place, and in how many ways he can place that minimum number.
为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘。这个长方形的池子被分成了M行N列个方格(1≤M,N≤30)。一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽、纯净、湛蓝的水。
贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上。
贝西的舞步很像象棋中的马步:每次总是先横向移动一格,再纵向移动两格,或先纵向移动两格,再横向移动一格。最多时,贝西会有八个移动方向可供选择。
约翰一直在观看贝西的芭蕾练习,发现她有时候不能跳到终点,因为中间缺了一些荷叶。于是他想要添加几朵莲花来帮助贝西完成任务。一贯节俭的约翰只想添加最少数量的莲花。当然,莲花不能放在石头上。
请帮助约翰确定必须要添加的莲花的最少数量,以及有多少种放置这些莲花的方法。
输入格式
【输入】
第一行:两个用空格分开的整数:M和N
第二行到M+1行:第i+1行有N个用空格分开的整数,描述了池塘第i行的状态:
0为水,1为莲花,2为岩石,3为贝西所在的起点,4为贝西想去的终点。
输出格式
【输出】
第一行:一个整数,需要增加的最少莲花数;如果无解,输出-1。
第二行:放置这些莲花的方案数量,保证这个数字不会超过一个64位的有符号整数,
如果第一行是-1,不要输出第二行。
输入输出样例
4 5 1 0 0 0 0 3 0 0 0 0 0 0 2 0 0 0 0 0 4 0
2 3
说明/提示
【样例说明】
池塘分成四行五列,贝西的起点在第二行第一列,想去的终点在第四行第四列,池
塘里一共有三朵莲花和一块石头。
最少需要两朵莲花,有三种方式可以放置,
页6 如下X所示:
10000 10X00 10X00
30X00 30000 3000X
00200 0X200 00200
0X040 00040 00040
题解:挑战紫题成功!撒花!!!
预处理出每个起点以及水位置的花费1个荷叶能到的地方
然后建边spfa大法好就ok了。(最后再求个最短路计数即可。
bh[i][j]=(i-1)*m+j; 用来算编号是真的强(我居然想了半天emmm)
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; const int N=900; const int oo=0x3f3f3f3f; typedef long long ll; int cnt,n,m,a[N][N],bh[N][N],jjj,yxr; int dx[18]={2,2,-2,-2,1,-1,1,-1}; int dy[18]={-1,1,-1,1,2,2,-2,-2}; int vi[N][N],vis[N*N],dis[N*N],head[N*N]; ll ans[N*N]; struct node{ int next; int to; }e[N*N]; void add(int x,int y){ e[++cnt].to=y; e[cnt].next=head[x]; head[x]=cnt; } void dfs(int o,int x,int y){ vi[x][y]=1; for(int k=0;k<8;k++){ int xx=x+dx[k]; int yy=y+dy[k]; if(xx<1 || yy<1 || xx>n || yy>m || vi[xx][yy]) continue; if(a[xx][yy]==1) dfs(o,xx,yy); else { vi[xx][yy]=1; add(o,bh[xx][yy]); } } } void init(){ scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); bh[i][j]=(i-1)*m+j; if(a[i][j]==3) jjj=bh[i][j]; if(a[i][j]==4) yxr=bh[i][j]; } } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]==0 || a[i][j]==3) { memset(vi,0,sizeof(vi)); dfs(bh[i][j],i,j); } } queue<int>q; void SPFA(int s,int t){ memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); vis[s]=1; dis[s]=0; q.push(s); int x,v; ans[s]=1; while(!q.empty()){ x=q.front(); q.pop(); vis[x]=0; for(int i=head[x];i;i=e[i].next){ v=e[i].to; if(dis[v]>dis[x]+1){ dis[v]=dis[x]+1; ans[v]=ans[x]; if(!vis[v]) { q.push(v); vis[v]=1; } } else if(dis[v]==dis[x]+1) ans[v]+=ans[x]; } } } int main(){ freopen("1606.in","r",stdin); freopen("1606.out","w",stdout); init(); SPFA(jjj,yxr); if(dis[yxr]!=oo) printf("%d %lld",dis[yxr]-1,ans[yxr]); else printf("-1 "); return 0; }