bzoj1698 [Usaco2007 feb]lilypad Pond Lotus leaf Pond [BFS]

Source: Internet
Author: User

Description
为了便于牛们欣赏和锻炼,农夫JOHN在他的农场上新添加了一个美丽的池塘。 JOHN的池塘是一个长方形,他已经把它划分成了M行N列的小正方行 (1 <= M <= 30; 1 <= N <= 30). 某些正方行里是石头,另外一些则是特别结实的荷叶,其余则只有清水。 为了锻炼,Bessie想从一片荷叶跳到另外一片。她的每一次跳跃都是一个象棋中的马步:两行一列或一行两列。 JOHN看到了Bessie并且发现有时Bessie没有办法达到她的目标荷叶。他准备添加一些荷叶来让Bessie完成她的目标。当然,荷叶不能放在石头上。 帮助JOHN找出他最少要放多少片荷叶和他一共有多少种放最少片荷叶的方案。
Input
第1行: 两个整数, M 和 N。第2~M+1行: 第i+1包含N个数,分别为第i行的N个格子的情况。 0表示格子为空,1表示有一片荷叶,2表示格子里有石头,3表示此格子是Bessie的起点,4 表示此格子是Bessie的目标。
Output
第1行: 一个数,最少情况下需要添加的荷叶数目。如果没有方案存在,输出- 1。第2行: 一个数,达到最小值的方案总数。这个数保证不超过内设64位整数(long long/ int64)的大小。如果第一行是-1,不要输出此行。
Sample Input
4 51 0 0 0 03 0 0 0 00 0 2 0 00 0 0 4 0输入解释:池塘含4行5列。Bessie在第2行第1列并且想跳到第4行第4列。池塘里有1块石头和3片荷叶。
Sample Output
23输出解释:至少需要2片荷叶。一共有三种摆法:第4行第2列,第2行第3列第1行第3列,第3行第2列第1行第3列,第2行第5列 R1C2,R2C3  |   R1C3,R3C2  |   R1C3,R2C5 1 0 0 0 0  |   1 0 X 0 0  |   1 0 X 0 0 3 0 X 0 0  |   3 0 0 0 0  |   3 0 0 0 X 0 0 2 0 0  |   0 X 2 0 0  |   0 0 2 0 0 0 X 0 4 0  |   0 0 0 4 0  |   0 0 0 4 0
Solution

First ask: At least add a few pieces of Lotus leaf.
Very simple to build and then run SPFA:
Each point ( x , y ) Connect the edges in 8 directions, if the point (nx,ny) Is clear water, the edge right is 1, if the lotus leaf is the edge of 0. The starting and ending points are treated as water, and the final answer is 1.

First asked no difficulty, the focus of the second question: there are several ways to add Lotus leaf.
First, what if the shortest path number to the end point is required? Just judge when the SPFA is slack:

ifvalue == dist[nex]) {    tot[nex] += tot[cur];    push();}ifvalue < dist[nex]) {    value;    tot[nex] = tot[nex];    push();}

But this is the "Shortest path number", not "the number of schemes to add Lotus leaf" because there are lotus leaves in the original. So we're going to change the way we build the map:

    • Each water is connected to the surrounding water
    • For each lotus leaf, DFS has its 8 directions, and if it is the lotus leaf continues DFS, if it is clear water, then put it into a temporary array.
    • Thus, for each of the lotus leaf connected block has a temporary array, the array of points 22 connected edge.

That's it, SPFA's code doesn't have to be changed.
Alas, I wrote this fool for two days.

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <vector>#include <queue>using namespace STD;structedge{intTo, V; Edge () {} Edge (intAintb): to (a), V (b) {}};intN, M;int Map[ *][ *]; vector<Edge>edges[1005];intSX, SY, ex, EY;intdir[8][2] = {{1,2}, {1, -2}, {-1,2}, {-1, -2}, {2,1}, {2, -1}, {-2,1}, {-2, -1}};BOOLCheckintXintY) {if(X <0|| X >= N | | Y <0|| Y >= m)return false;if(Map[x] [Y] = =2)return false;return true;}intdist[1005];Long Longans[1005];BOOLvis[1005];voidBFS () { Queue<int>Qmemset(Dist,0x3f,sizeof(Dist));memset(ANS,0,sizeof(ans));memset(Vis,0,sizeof(VIS));ints = SX * m + sy;    Q.push (s); Dist[s] =0; Ans[s] =1; Vis[s] =1; while(!q.empty ()) {intcur = Q.front (); Q.pop (); for(inti =0; I < edges[cur].size (); i++) {Edge e = edges[cur][i];if(Dist[cur] + e.v = = Dist[e.to]) {Ans[e.to] + = Ans[cur];if(!vis[e.to]) {if(E.to! = ex * m + ey) {Vis[e.to] =1;                    Q.push (e.to); }                }            }if(Dist[cur] + e.v < dist[e.to])                {Dist[e.to] = Dist[cur] + e.v; Ans[e.to] = Ans[cur];if(!vis[e.to]) {if(E.to! = ex * m + ey) {Vis[e.to] =1;                    Q.push (e.to); }                }            }        }    }}inttmp[1005], tot =0;intvis2[1005];intused[1005][1005];voidDfsintXintY) { for(inti =0; I <8; i++) {intNX = x + dir[i][0];intNY = y + dir[i][1];if(!check (NX, NY))Continue;if(!Map[NX] [NY] &&!VIS2[NX * m + ny]) {VIS2[NX * m + NY] =1;        tmp[tot++] = NX * M + NY; }if(Map[NX] [NY] = =1&&!vis[nx * m + ny]) {VIS[NX * m + NY] =1;        DFS (NX, NY); }    }}intMain () {scanf("%d%d", &n, &m);intT for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {scanf("%d", &MapI [j]);if(MapI [j] = =3) {SX = i; sy = j;MapI [j] =0; }if(MapI [j] = =4) {ex = i; EY = j;MapI [j] =0; }        }    } for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {if(MapI [j])Continue; for(intK =0; K <8; k++) {intNX = i + dir[k][0];intNY = j + dir[k][1];if(Check (NX, NY) &&!Map[NX]                    [NY]) Edges[i * m + j].push_back (Edge (NX * m + NY,1)); }        }    } for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {if(MapI [j] = =1&&!vis[i * m + j]) {tot =0;memset(Vis2,0,sizeof(VIS2)); Vis[i * j +1] =1; DFS (I, j); for(intK =0; K < tot; k++) { for(intL = k +1; l < tot; l++) {if(!used[tmp[k]][tmp[l]]) {Used[tmp[k]][tmp[l]] = used[tmp[l]][tmp[k]] =1; Edges[tmp[k]].push_back (Edge (tmp[l),1)); Edges[tmp[l]].push_back (Edge (tmp[k),1)); }}}}} (BFS);if(Dist[ex * m + ey] = =0x3f3f3f3f) {printf(" -1\n");return 0; }printf("%d\n%lld\n", Dist[ex * m + ey]-1, Ans[ex * m + ey]);return 0;}

bzoj1698 [Usaco2007 feb]lilypad Pond Lotus leaf Pond [BFS]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.