firenet

#include <iostream>



using namespace std;



struct group_inf

{

    int index;

    int num;

    int r;

    int c;

};



void run(int x, int y, char **t, group_inf*g, int length, int cur_n, int &maxnum, int num)

{

    if (cur_n > length) return;

    int n = num;



    //判断当前点的位置

    int cx, cy = 0;

    cx = g[cur_n].r;

    cy = g[cur_n].c;

    for (int k = 0; k<g[cur_n].num; k++, cy++,n=num)

    {

        //保存上层现场

        char **cur_t;

        cur_t = new char*[x];

        for (int i = 0; i<x; i++)

            cur_t[i] = new char[y];

        for (int i = 0; i<x; i++)

        for (int j = 0; j<y; j++)

            cur_t[i][j] = t[i][j];



        if (cur_t[cx][cy] == '.')

        {

            n++;

            if (n>maxnum) maxnum = n;

            //填攻击位置

            for (int i = cx; i < x; i++)

            if (cur_t[i][cy] == '.') cur_t[i][cy] = '*';

            else if (cur_t[i][cy] == 'X') break;

            for (int i = cx; i >= 0; i--)

            if (cur_t[i][cy] == '.') cur_t[i][cy] = '*';

            else if (cur_t[i][cy] == 'X') break;

            for (int i = cy; i < y; i++)

            if (cur_t[cx][i] == '.') cur_t[cx][i] = '*';

            else if (cur_t[cx][i] == 'X')break;

            for (int i = cy; i >= 0; i--)

            if (cur_t[cx][i] == '.') cur_t[cx][i] = '*';

            else if (cur_t[cx][i] == 'X')break;

            //传递到下一层

            

            run(x, y, cur_t, g, length, cur_n + 1, maxnum, n);

        }

    }



}



void makegroup(char**t, group_inf*g, int &length, int x, int y)//分组

{

    char **cur_t;

    cur_t = new char*[x];

    for (int i = 0; i<x; i++)

        cur_t[i] = new char[y];

    for (int i = 0; i<x; i++)

    for (int j = 0; j<y; j++)

        cur_t[i][j] = t[i][j];

    int n = -1;

    for (int i = 0; i<x; i++)

    {

        for (int j = 0; j<y; j++)

        if (cur_t[i][j] == '.')

        {

            int temp = 0;

            for (int k = j; k<y; k++, temp++)

            if (cur_t[i][k] == 'X')

            {

                n++;

                g[n].index = n;

                g[n].num = temp;

                g[n].r = i;

                g[n].c = k - temp;

                

                break;

            }

            else if (k == y - 1)

            {

                n++;

                cur_t[i][k] = '*';

                g[n].index = n;

                g[n].num = temp + 1;

                g[n].r = i;

                g[n].c = k - temp;

                

            }

            else

                cur_t[i][k] = '*';

        }

    }

    length = n;

}



int main()

{

    int n;

    cin >> n;

    while (n != 0)

    {

        char**t;

        t = new char*[n];

        for (int i = 0; i<n; i++)

            t[i] = new char[n];

        //输入

        for (int i = 0; i<n; i++)

        for (int j = 0; j<n; j++)

            cin >> t[i][j];

        group_inf g[16];

        int num = 0;

        int length = 0;

        makegroup(t, g, length, n, n);

        run(n, n, t, g, length, 0, num, 0);

        cout << endl << num << endl;

        cin >> n;

    }

    return 0;

}

先分组  自定义group_inf存储每组的信息

index组号 num组内成员个数 r c 起始位置 

再用递归暴力求解  遍历了每一种情况

挺繁琐的方法 毕竟是zoj的第一次尝试0 0

你可能感兴趣的:(net)