UVAlive 6693 Flow Game(模拟)

62 篇文章 0 订阅
9 篇文章 0 订阅
题目地址: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值