@noi - 172 @ elephant hunt


@description@

There is an elephant on a piece of plain.

Plain is divided into n × m squares. Elephant is located in the initial (1,1). Every second, elephants moved to a neighboring grid (four communication), but does not move to the outside of the plain. Because you have low vision, you can not know what the elephant moved each time to the grid.

You can use the fireball to attack ground. Every release fireball, you can attack any number of squares. Each grid can only be attacked once. Elephants can not move to be attacked on the grid, if the grid adjacent to the elephant can not move, then it will stay still intact.

Your goal is to last only a fireball grid has not been attacked, and then you go there to capture the elephant. Prior to this, you must ensure that your Fireball will not hit an elephant. Because elephants fighting is very strong, and only in certain grid you can catch an elephant.

However, because of your magic level is not high, before use fireball you need to be ready. First ready time is at least n + m seconds, then each time ready to be longer than the last. Gaining time must be an integer seconds. You want to minimize the time of the last ready.

Note: every second elephant to move first, and then you will release the fireball. That is, if the first time you are ready for the time n + m seconds, then the elephants have been moved n + m times.

input
first line contains two integers n, m. n, m ≤ 1000.

Next, the number per row m n rows, each number can be 0 or 1. The first number i + 1 row j-th column represents (i, j) in the grid, you can seize the Elephant (1 denotes 0 means not).

output
output number of lines, each line represents the release of a fireball.

Each line represents a first integer ready time, the next integer number of lattice attack lattice i-th row j-th column number is (i-1) * m + j. Each line of the final output of a 0.

If no solution output line an integer -1.

sample input
3 3
1 1 1
1 1 1
1 1 1
sample output
7 1 3 7 9 0
9 2 4 6 8 0

@solution@

Elephants too strong.

Among rectangular stained marker (1, 1) is white, then a certain time or elephant Haig either white cells.
Let's assume that white elephant in the grid, you can only delete Haig, and to ensure that deleted after completion of the remaining white grid connectivity was still legal. Delete the last remaining grid is finished.
We can delete the grid reversed course, start out the same color add a grid from a grid, and maintain connectivity. So you can get every time we need to delete the grid of numbers.

If we ready time is even, then black and white color Elephant's position unchanged; otherwise position to the opposite color.
We know by deleting the first color grid, elephants and the initial position of (1, 1) and white cells, may be determined for the first time is an odd number or an even poised ready.
If you delete the grid after a certain number of elephants in the lattice of a certain color. You can get on a different color of the grid must be deleted, this time with the elephants should delete grid where the grid with color, it is necessary to leave the elephants.
So get: In addition to gaining the first time parity needs to determine what the rest of the time is always an odd number ready.

Can be found to a certain point, the first time by deleting certainly include the four corners of a rectangular one. So we can ask for this point to the maximum of four corners of the Manhattan distance, and determines the first parity and the (m + n) is the same parity, can be introduced in the final state the point of the answer is How many.
All legitimate points are solving the corresponding answers, and take the minimum. Output is easy to program, along the lines of Gag child to think you can, or can also be thinking in terms of Manhattan distance of each point to the end point.

Complexity of O (nm).

@accepted code@

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 1000;
int n, m, x, y, cnt = 0;
int fun(int x, int y) {
    return max(max(x-1 + y-1, n-x + m-y), max(x-1 + m-y, n-x + y-1));
}
int abs(int x) {return x > 0 ? x : -x;}
vector<int>ans[2*MAXN + 5];
int X[MAXN*MAXN + 5], Y[MAXN*MAXN + 5];
int main() {
    scanf("%d%d", &n, &m), x = -1, y = -1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) {
            int a; scanf("%d", &a);
            if( a ) {
                if( x == -1 && y == -1 )
                    x = i, y = j;
                else if( fun(x, y) > fun(i, j) )
                    x = i, y = j;
                else if( fun(x, y) == fun(i, j) ) {
                    if( ( (((x + y)&1) ^ (fun(x, y)&1)) == ((n + m)&1) ) && ( (((i + j)&1) ^ (fun(i, j)&1)) != ((n + m)&1) ) )
                        x = i, y = j;
                }
            }
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            ans[abs(i-x)+abs(j-y)].push_back(++cnt), X[cnt] = i, Y[cnt] = j;
    int p = n + m; bool flag = true;
    for(int i=n+m;i>=1;i--) {
        if( ans[i].empty() ) continue;
        if( flag ) {
            p += ((X[ans[i][0]]-1+Y[ans[i][0]]-1)&1)^((n+m)&1)^1, flag = false;
            printf("%d", p);
            if( p % 2 == 0 ) p--;
        }
        else {
            p += 2;
            printf("%d", p);
        }
        for(int j=0;j<ans[i].size();j++)
            printf(" %d", ans[i][j]);
        printf(" %d\n", 0);
    }
}

@details@

Although the solution to a problem is easy to understand Ah, the code is not hard to write, but to think through the perspective of the child in turn Gag really can not think ah. . .

The examination room just write a constructor 50 points. . .

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11115295.html