Codeforces 993A. Two Squares(暴力求解)

解题思路(暴力解法)

  1. 平行于x轴的正方形和与x轴成45度倾斜的正方形相交的点中必定有整数点。即若两正方形相交,必定存在整数i,j,使(i,j)同时属于两个正方形。
  2. 我们把两个正方形中的整数点都找出来,看一下有没有重复点,就可以判断是否相交。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct point{
    int x;int y;
}sq[4],sp[4];

bool cmp(point a,point b){
    if(a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

//t[i][j]=1表示第一个正方形包含这个点。
int t[220][220];
void tian(point a,point b){
    for(int i = a.x;i <= b.x; ++i){
        for(int j = a.y;j <= b.y; ++j){
            t[i][j] = 1;
        }
    }
}

//判断第二个正方形是否包含t[i][j]为1的点(i,j)
bool judge(point* sp){
    for(int i = sp[0].x;i <= sp[1].x; ++i){
        for(int j = 0;j <= i-sp[0].x; ++j){
            if(t[i][sp[0].y+j] == 1 or t[i][sp[0].y-j] == 1){
                return true;
            }
        }
    }
    for(int i = sp[1].x;i <= sp[3].x; ++i){
        for(int j = 0;j <= sp[2].y-sp[0].y-(i-sp[1].x); ++j){
            if(t[i][sp[0].y+j] == 1 or t[i][sp[0].y-j] == 1){
                return true;
            }
        }
    }
    return false;
}

int main(){
    for(int i = 0;i < 4; ++i) cin >> sq[i].x >> sq[i].y, sq[i].x+=100,sq[i].y+=100;
    for(int i = 0;i < 4; ++i) cin >> sp[i].x >> sp[i].y, sp[i].x+=100,sp[i].y+=100;
    sort(sq,sq+4,cmp);
    sort(sp,sp+4,cmp);
    tian(sq[0],sq[3]);
    if(judge(sp))  cout << "YES" << endl;
    else  cout << "NO" << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/zhangjiuding/p/9192583.html

时间: 2024-08-05 18:09:31

Codeforces 993A. Two Squares(暴力求解)的相关文章

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

求解最大子数组问题 -- 暴力求解 和 分治法求解

/*------------------ 求解最大子数组问题 --------------- 最大子数组,就是求解一个数组的所有元素的各种组合中,和最大的 那个子数组.在这种情况下,如果元素值全部非负,那么最大子数组当然 是所有元素.但是如果有负值的元素存在,那么久需要找到一个由数组中 连续几个元素组成的子数组并且其元素值之和最大. */ #include <iostream> using namespace std; struct ArrayStruct { ArrayStruct(int

BZOJ 1054题解 BFS暴力求解

BZOJ 1054题解 BFS暴力求解 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1884  Solved: 1033[Submit][Status][Discuss] Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移 动到某人

Codeforces 57C Array dp暴力找规律

题目链接:点击打开链接 先是计算非递增的方案, 若非递增的方案数为x, 则非递减的方案数也是x 答案就是 2*x - n 只需求得x即可. 可以先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元即可. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #def

Codeforces 57B Martian Architecture 暴力||线段树

题目链接:点击打开链接 题意:n长的序列(初始全为0) m个操作 k个查询 下面m个操作[l,r] h 代表 a[l] +=h; a[l+1] += h+i; a[l+i] += h+i;  l<=i<=r 然后问k个位置的和 因为k<=100 所以直接暴力也可以 ----------------------- 如果k<=100000 也是可以做的 只需要给区间记录一个标记lazy,表示从左端点开始 l, l+1, l+i ··· l+r 而向下更新时, 左区间则直接更新, 右区间

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerou

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655

codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不重复) 1.交换两列,对于整个矩阵只能操作一次 2.每行交换两个数. 交换后是否可以使每行都是1-m 数字递增. 解题思路: 1.得到矩阵后先判断,是否每行可以交换两个数可以得到递增的矩阵,如果可以则输出“YES”. 2.暴力交换两列,交换两列后,判断每行是否可以交换两个数得到递增的矩阵,如果可以则