ZOJ 1002 C / C ++


The topic takes into account backtracking, deep search

Topic interpretation

Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

The goal is to find the most suitable placement

A bullet can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other.

The bullet can be shot in four directions, and the
wall can block the flying of the bullet. The
requirement is to place the position so that the bullet will not hit the position where the building is placed, as shown in the
figure:

insert image description here

What can be learned from this solution

Represents a two-dimensional array in addition to defining a two-dimensional array loop input number,

It can also be grouped by the characteristics of remainder and quotient in programming,

        x = k / n;//横向坐标每n个是相同的
        y = k % n;//纵向坐标每n个为一组

For example, the x-axis in the title adopts the method of quotient, which uses the characteristic of quotient to retain integer bits, and uses n of the denominator as the base, and each n is a group.

The y-axis uses the feature of calculating the remainder and taking the remainder, and also groups y into a group of n, but the difference is that the number of n in each group is different from each other and increases regularly.

AC code

#include<stdio.h>
int n;
char map[4][4];//题目要求最大4*4
int bestn;//标识符,标记放过炮塔的位置
int canput(int x, int y)
{
    
    
    int i;
    for (i = x - 1;i >= 0;i--)
    {
    
    
        if (map[i][y] == 'X')
        {
    
    
            break;
        }
        if (map[i][y] == 'o')
        {
    
    
            return 0;
        }
    }
    for (i = y - 1;i >= 0;i--)
    {
    
    
        if (map[x][i] == 'X')
        {
    
    
            break;//wall直接退出判断
        }
        if (map[x][i] == 'o')
        {
    
    
            return 0;//为假结束判断
        }
    }
    return 1;//为真结束判断
}
//K表示放置炮塔的位置 
void backtrack(int k, int current)
{
    
    
    int x, y;
    if (k >= n * n)//k设置在1--n*n之间
    {
    
    
        if (current > bestn)
        {
    
    
            bestn = current;//赋予最大值
        }
        return;
    }
    else
    {
    
    
        x = k / n;//横向坐标每n个是相同的
        y = k % n;//纵向坐标每n个为一组
        if (map[x][y] == '.' && canput(x, y))//如果return 1 则继续执行if语句
        {
    
    
            map[x][y] = 'o';
            backtrack(k + 1, current + 1);//执行顺序为(0,1)(0,2)....
            map[x][y] = '.';
        }
        backtrack(k + 1, current);
    }
}
int main()
{
    
    
    while (scanf("%d", &n) != EOF)
    {
    
    
        int i, j;
        bestn = 0;
        for (i = 0;i < n;i++)
        {
    
    
            for (j = 0;j < n;j++)
            {
    
    
                char ch;
                ch = getchar();
                if (ch == '\n')
                {
    
    
                    j--;
                    continue;
                }
                else
                {
    
    
                    map[i][j] = ch;
                } 
            }
        }
        backtrack(0, 0);
        printf("%d\n", bestn);
        scanf("%d", &n);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_50767141/article/details/117885585
ZOJ
ZOJ