ZOJ 2412 Farm Irrigation

Farm Irrigation
Time Limit: 1 Second      Memory Limit: 32768 KB

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.


Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

then the water pipes are distributed like


Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

Input

There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample Input

2 2
DK
HF

3 3
ADC
FJK
IHE

-1 -1

Sample Output

2
3

 

这道题试了三次才通过,还是看了别人的博客才知道自己哪里错啦

 

应该是x1=r/m,自己却写得是x1=r/n;看了很长时间都没找出来是哪儿错……抑郁了很长时间

 

这道题的思路是:和迷宫算法差不多,就是四个方向都遍历一遍,访问过的地方设访问标记,说明已访问

 

过,避免重复访问。还有迷宫算法会在回溯的时候,把已经设置访问标记的地方恢复到原始状态。而这道题

 

并不需要,这应该是本题与迷宫算法的一点小小的区别……

 

本题与迷宫算法最大的区别是:迷宫算法会输入中告诉你,起点和终点的位置;但本题的主旨是让我们寻找

 

到每个位置上所能连接到得最大长度,并没有告诉我们起点和终点。这就意味,图中的每个点都可能是起点

 

或是终点。所以,需要以每个点为起点遍历一遍。这时候又出现了另一个问题,那就是,一个点可能被遍历

 

多次,我们通过设置访问标记就可以解决这个问题,要求每个点只能被访问一次。

 

还有一点是,图中的点既可以用一维表示,也可以用二维表示

 

用一维表示时需要注意的问题是:下标需从0开始

 

横坐标=下标数/列数;     x=i/m;

 

纵坐标=下标数%列数;    y=i%m;

 

 

#include<stdio.h>

#define N 50

int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int s[4]={2,3,0,1};
int a[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
char b[N][N];
int c[N][N][4],d[N][N];
int n,m;
int cc,bestcc;

void backtrack(int x,int y)
{
int k=0,px,py;
while(k<4)
{
   px=x+dx[k];
   py=y+dy[k];
   if(px<n&&px>=0&&py<m&&py>=0&&d[px][py]&&c[x][y][k]&&c[px][py][s[k]])
   {
    d[px][py]=0;
    cc++;
    c[x][y][k]=c[px][py][s[k]]=-1;
    backtrack(px,py);
   }
   k++;
}  
}


int main()
{
   int x1,y1;
     while(scanf("%d %d",&n,&m)!=EOF)
   {
          if(n==-1&&m==-1) break;
    for(int i=0;i<n;i++)
      scanf("%s",b[i]);
    for(int i=0;i<n;i++)
     for(int j=0;j<m;j++)
      d[i][j]=1;
    for(int i=0;i<n;i++)
     for(int j=0;j<m;j++)
      for(int k=0;k<4;k++)
       c[i][j][k]=a[b[i][j]-65][k];
    int r=0;
    bestcc=0;
            while(r<n*m)
            {
     x1=r/m;
     y1=r%m;
     cc=0;
     if(d[x1][y1])
     {
      d[x1][y1]=0;
      backtrack(x1,y1);
      bestcc+=cc;
       }
     r++;
    }
      printf("%d/n",n*m-bestcc);
   }
   return 0;
}

 

这是别人的程序,可以借鉴的

 

#include<stdio.h>
#include<string.h>
char pipe[20][4]={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,1}};
char inpipe[55][55];
char flag[2555];
int N,M;
int count;
char FLAG;

void DFS(int i)
{
   
        if(!flag[i])
        {
            int x=i/M,y=i%M;          
            if(!FLAG)    {FLAG=2;count++;}    //每一条路记录一次
            flag[i]=1;
            if(y+1<M&&!flag[i+1]&&pipe[inpipe[x][y]-'A'][2]&&pipe[inpipe[x][y+1]-'A'][0])
            {
                DFS(i+1);
            }

            if(x+1<N&&!flag[i+M]&&pipe[inpipe[x][y]-'A'][3]&&pipe[inpipe[x+1][y]-'A'][1])
            {
                DFS(i+M);
            }

            if(y-1>-1&&!flag[i-1]&&pipe[inpipe[x][y]-'A'][0]&&pipe[inpipe[x][y-1]-'A'][2])
            {
                DFS(i-1);
            }

            if(x-1>-1&&!flag[i-M]&&pipe[inpipe[x][y]-'A'][1]&&pipe[inpipe[x-1][y]-'A'][3])
            {
                DFS(i-M);
            }
        }       


}


int main()
{
    int i;
   
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        count=0;
        FLAG=0;
        if(M<=0||N<=0) return 0;
            for(i=0;i<N;i++)
                scanf("%s",inpipe[i]);
            for(i=0;i<M*N;i++)
            {
                FLAG=0;
                DFS(i);
            }
                printf("%d/n",count);
            memset(flag,0,sizeof(flag));
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值