第十三届校赛之相交的矩形(1221)(水题,暴力一下就可以了)

来源:互联网 发布:王牌特工特工学院淘宝 编辑:程序博客网 时间:2024/06/05 20:52

给出两个平行于坐标轴的矩形A、B。求A、B是否相交:如果两个矩形四条边上有任何一点重合,则输出YES,否则输出NO。
Description
多组测试数据(不超过1000组)。 
第一行四个整数,x1、y1、x2、y2,(x1,y1)、(x2,y2)表示A矩形对角上的两个点; 
第二行四个整数,x3、y3、x4、y4,(x3,y3)、(x4,y4)表示B矩形对角上的两个点。 
数据保证A、B矩形是存在的,并且矩形坐标绝对值不大于100。
Input
每组测试数据输出对应的结果。
Output
1
2
3
4
1 1 3 3
2 2 4 4
1 1 2 2
3 3 4 4
Sample Input
1
2
YES
NO

/**题解:用一个mp存矩形的四条边,第一个矩形标记1,第二个矩形标记为2;暴力搜索一下就可以AC了坑点是可能出现负数*/#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>#include<stdlib.h>#include<string.h>using namespace std;struct node{int x, y;};int mp[405][405];//y行x列void fcol(int ss, int tt,int col,int num)//第几列ss-tt{int _min = min(ss, tt);int _max = max(ss, tt);for (int i = _min; i <= _max; i++){mp[i][col]=num;}}void frow(int ss, int tt,int row,int num){int _min = min(ss, tt);int _max = max(ss, tt);for (int i = _min; i <= _max; i++){mp[row][i]=num;}}void frec(node rec1[5],int num){frow(rec1[1].x, rec1[3].x, rec1[1].y,num);fcol(rec1[1].y, rec1[3].y, rec1[3].x,num);frow(rec1[1].x, rec1[3].x, rec1[3].y,num);fcol(rec1[1].y, rec1[3].y, rec1[1].x,num);}int main(){node rec1[5], rec2[5];while (cin >> rec1[1].x >> rec1[1].y >> rec1[3].x >> rec1[3].y){cin >> rec2[1].x >> rec2[1].y >> rec2[3].x >> rec2[3].y;//有可能出现负数rec1[1].x += 100;rec1[1].y += 100;rec2[3].x += 100;rec2[3].y += 100;rec1[3].x += 100;rec1[3].y += 100;rec2[1].x += 100;rec2[1].y += 100;memset(mp, 0, sizeof(mp));//第一个矩形标记1frec(rec1,1);frec(rec2,2);int flag = 0;for (int i = min(rec1[1].y, rec1[3].y); i <= max(rec1[1].y, rec1[3].y)&&!flag; i++){if (mp[i][rec1[1].x] == 2 || mp[i][rec1[3].x] == 2){flag = 1;}}for (int i = min(rec1[1].x, rec1[3].x); i <= max(rec1[1].x, rec1[3].x)&&!flag; i++){if (mp[rec1[1].y][i] == 2 || mp[rec1[3].y][i] == 2){flag = 1;}}if (flag){cout << "YES\r\n";}else{cout << "NO\r\n";}}return 0;}




阅读全文
0 0
原创粉丝点击