AtCoder:Connected?(思维)

E - Connected?


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

Snuke is playing a puzzle game. In this game, you are given a rectangular board of dimensions R×C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (xi,1,yi,1) and (xi,2,yi,2).

The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N. Here, the curves may not go outside the board or cross each other.

Determine whether this is possible.

Constraints

  • 1R,C108
  • 1N105
  • 0xi,1,xi,2R(1iN)
  • 0yi,1,yi,2C(1iN)
  • All given points are distinct.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

R C N
x1,1 y1,1 x1,2 y1,2
:
xN,1 yN,1 xN,2 yN,2

Output

Print YES if the objective is achievable; print NO otherwise.


Sample Input 1

Copy
4 2 3
0 1 3 1
1 1 4 1
2 0 2 2

Sample Output 1

Copy
YES

The above figure shows a possible solution.


Sample Input 2

Copy
2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1

Sample Output 2

Copy
NO

Sample Input 3

Copy
5 5 7
0 0 2 4
2 3 4 5
3 5 5 2
5 5 5 4
0 3 5 1
2 2 4 4
0 5 4 1

Sample Output 3

Copy
YES

Sample Input 4

Copy
1 1 2
0 0 1 1
1 0 0 1

Sample Output 4

Copy
NO
题意:一个r*c的矩阵,有1~n个数字,每个数字出现在(xi,1,yi,1)(xi,2,yi,2)上要求将所有相同的数字连起来,但连线不可以相交和越出矩阵,问能否实现。

思路:显然某个点两边都在矩阵边缘上才要考虑,那么将这类点找出来,按矩阵边缘顺时针排序,然后遍历,和栈顶数字相同就出栈,没出现过的就压进栈,最后判断栈是否为空即可,栈不空说明有线相交了。

# include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5+3;
int row, col, n, que[maxn<<1];
pair<int, int> arr[maxn<<1];

bool check(int a, int b)
{
    if(a==0 || a==row || b==0 || b==col) return true;
    return false;
}

int cal(int a, int b)
{
    if(a==0) return b;
    else if(b==col) return col + a;
    else if(a==row) return col + row + col - b;
    else return col + col + row + row - a;
}

int main()
{
    int a, b, c, d, cnt = 0, icount = 0;
    scanf("%d%d%d",&row,&col,&n);
    {
        while(n--)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(check(a, b) && check(c, d))
            {
                arr[cnt++] = make_pair(cal(a, b), icount);
                arr[cnt++] = make_pair(cal(c, d), icount++);
            }
        }
        sort(arr, arr+cnt);
        int r=0;
        for(int i=0; i<cnt; ++i)
        {
            if(!r)
                que[++r] = arr[i].second;
            else if(arr[i].second == que[r])
                --r;
            else
            que[++r] = arr[i].second;
        }
        if(!r) puts("YES");
        else puts("NO");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值