UVAlive 6693 Flow Game(模拟)

来源:互联网 发布:网络安全教育考试题库 编辑:程序博客网 时间:2024/06/06 07:13
题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4705

思路:若有解,两点连线最小距离=曼哈顿距离+1,则ans=abs(x1-x2)+abs(y1-y2)+abs(x3-x4)+abs(y3-y4)+2。若无解,则两线相交:对于边界,逆时针遍历,若1212或2121则此时无解,其他情况有解。注意所有点在同一行或同一列特判:+2(1 2 2 1)或不加(1 1 2 2)。

#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct point{    int x, y;};int n;const int maxn=20000+10;char ch[11][11];int res[10];int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(ch, 0, sizeof(ch));        memset(res, 0,sizeof(res));        scanf("%d",&n);        getchar();        point a1,a2,b1,b2;        bool flag1 = false, flag2 = false;        for(int i = 0; i < n; i++)        {            for(int j = 0; j < n; j++)            {                scanf("%c",&ch[i][j]);                if(ch[i][j] == '1' && !flag1)                    a1.x = i, a1.y = j, flag1 = true;                else if(ch[i][j] == '1')                    a2.x = i, a2.y = j;                if(ch[i][j] == '2' && !flag2)                    b1.x = i, b1.y = j, flag2 = true;                else if(ch[i][j] == '2')                    b2.x = i, b2.y = j;            }            getchar();        }//        cout << "******************\n";//        for(int i = 0; i < n; i++)//        {//            for(int j = 0; j < n; j++)//                printf("%c",ch[i][j]);//            printf("\n");//        }//        cout << "******************\n";//        cout << a1.x << " " << a1.y << "****" << a2.x << " " << a2.y << endl;//        cout << b1.x << " " << b1.y << "****" << b2.x << " " << b2.y << endl;        int cur = 0;        for(int i = 0; i < n; i++)            if(ch[i][0] != '.')                res[cur++] = ch[i][0] - '0';        for(int i = 1; i < n; i++)            if(ch[n - 1][i] != '.')                res[cur++] = ch[n - 1][i] - '0';        for(int i = n - 2; i >= 0; i--)            if(ch[i][n - 1] != '.')                res[cur++] = ch[i][n - 1] - '0';        for(int i = n - 2; i >= 1; i--)            if(ch[0][i] != '.')                res[cur++] = ch[0][i] - '0';        bool ok = true;//        cout << cur << endl;//        cout << res[0] << " " << res[1] << " " << res[2] << " " << res[3] << endl;        if(res[0] == 1 && res[1] == 2 && res[2] == 1 && res[3] == 2)            ok = false;        else if(res[0] == 2 && res[1] == 1 && res[2] == 2 && res[3] == 1)            ok = false;        if(!ok)        {            printf("-1\n");            continue;        }        int ans = 0;        ok = false;        ans += abs(a1.x - a2.x) + abs(a1.y - a2.y) + 1;        ans += abs(b1.x - b2.x) + abs(b1.y - b2.y) + 1;        if(a1.x == a2.x && a2.x == b1.x && b1.x == b2.x)        {            if(a1.y > a2.y)            {                point tmp = a1;                a1 = a2;                a2 = tmp;            }            if(b1.y > b2.y)            {                point tmp = b1;                b1 = b2;                b2 = tmp;            }            if(!(a2.y < b1.y || b2.y < a1.y))                ok = true;        }        else if(a1.y == a2.y && a2.y == b1.y && b1.y == b2.y)        {            if(a1.x > a2.x)            {                point tmp = a1;                a1 = a2;                a2 = tmp;            }            if(b1.x > b2.x)            {                point tmp = b1;                b1 = b2;                b2 = tmp;            }            if(!(a2.x < b1.x || b2.x < a1.x))                ok = true;        }        if(ok) ans += 2;        printf("%d\n",ans);    }}




0 0
原创粉丝点击