编程介的小学生 2019-08-28 17:44 采纳率: 0.4%
浏览 82

C语言 Everyone is No.1

Problem Description
Everyone is No.1------ As long as there is belief in my heart, I will hold on until the last moment. Real hero, spirit never die!

See Figure 1, Teddy was in the left-up point, a ‘E’ represents a hole. Now, Teddy want to fill in all the hole. He has a special soil gun. When he shot, it can spread right or down at any time and fill the hole when it get through it, every shot must stop at the right-down point. See figure 2, Teddy should shot three times, but the more optimal solution was in the figure 3.

Figure 1

Figure 2 Figure 3

Your task is to create a program that will determine the minimum number shots needed to fill all the hole, if you can’t have any solution about it, don’t fell sorry. Because Everyone is No.1, you can solve other problems first , then return to this problem, you will see it can be so easy.

Input
The input consists of one or more field maps. A field map consists of two parts:
Firstly, two integers n, m ( 1 <= n, m <= 24)
Secondly, a nXm matrix represent the map.

‘E’ represent the empty hole.
‘.’ represent the soil.

Output
The output will consist of a single line for each field map containing the minimum number of shot needed to fill in all the empty hole.

Sample Input
6 7
.E.E...
...E.E.
.......
...E..E
.......
.....E.

2 2
EE
EE

Sample Output
2
2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-31 08:38
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    #include <stdio.h>
    int main(){
        int i,j;
        int n,m;
        scanf("%d%d",&n,&m);
        int a[n][m];
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                scanf(" %c",&a[i][j]);
            }
        }
        int res[5]={};
        int sum=0;
        int min_shot=INT_MAX;
        //printf("res:%d\n",res);
        for(int i=0;i<=n-1;i++){
            for(int j=0;j<=m-1;j++){
                int k=i+min_shot;
                int l=j+min_shot;
                int t=k*m+j;
                int f=k*m+l;
                int x,y;
                if(a[k][l]=='.'){
                    res[0]++;
                    res[1]++; 
                    continue;
                }
                if(k==i){
                    y=l;
                    x=t;
                }else{
                    y=k;
                    x=f;
                }
                if(a[y][x]=='.' && a[t][y]=='.' && a[f][x]=='.' && a[k][l]!='E'){
                    res[0]++;
                    res[1]++;
                    res[2]=1;
                }else{
                    res[0]+=k-m;
                    res[1]+=m-n;
                    res[2]=max(res[2],1);
                }
                if(min_shot>res[2]){
                    min_shot=res[2];
                }
            }
        }
        printf("%d\n",min_shot);
        return 0;
    }
    
    评论

报告相同问题?