Lake Counting(dfs) + Catch That Cow(bfs)

【一】

Lake Counting

TimeLimit:1000MS  MemoryLimit:65536K

64-bit integer IO format:%lld

Problem Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

SampleInput

10 12 
W........WW. 
.WWW.....WWW
....WW...WW. 
.........WW. 
.........W.. 
..W......W.. 
.W.W.....WW. 
W.W.W.....W. 
.W.W......W. 
..W.......W.

SampleOutput

3

【题解】

从任意的'W'开始将周围的八个方块包括自己九个方块变成'.',并把八个方块中为'W'的方块的位置做重复操作,记录这样变化的次数并输出。

【代码】

#include<stdio.h>
char a[105][105];
int n,m;
void dfs(int x,int y)
{

    for(int i=-1;i<=1;i++)
        for(int j=-1;j<=1;j++)
        {
            int xx=x+i,yy=y+j;
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]=='W')
            {
                a[xx][yy]='.';
                dfs(xx,yy);
            }
        }
}
main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",a[i]);
    int ans=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(a[i][j]=='W')
            {
                dfs(i,j);
                ans++;
            }
    printf("%d\n",ans);
}

【二】

Catch That Cow

TimeLimit:2000MS  MemoryLimit:32768KB

64-bit integer IO format:%I64d

Problem Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

多组数据

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

SampleInput

5 17

SampleOutput

4 

Hint   The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

【题解】

显然如果n>=k 那么只能采取x-1的方式到达k。总共有三种走法分别为x+1,x-1,2*x,判断范围和是否走过,直接bfs即可。

【代码】
 

#include<bits/stdc++.h>
using namespace std;
int n,k;
int vis[100005];
struct p{
    int x,c;
}st,ed;
int bfs()
{
    queue <p> q;
    while(!q.empty())
        q.pop();
    st.x=n,st.c=0;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        for(int i=1;i<=3;i++)
        {
            ed.c=st.c+1;
            if(i==1)
               ed.x=st.x+1;
            else if(i==2)
                ed.x=st.x-1;
            else
                ed.x=st.x*2;
            if(ed.x==k)
                return ed.c;
            if(ed.x>=0&&ed.x<=100000&&vis[ed.x]==0)
            {
                vis[ed.x]=1;
                q.push(ed);
            }
        }
    }
}
main()
{
    while(~scanf("%d%d",&n,&k))
    {
        if(n>=k)
            printf("%d\n",n-k);
        else
        {
            memset(vis,0,sizeof(vis));
            printf("%d\n",bfs());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41117236/article/details/81198256