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
- UVAlive 6693 Flow Game(模拟)
- UVALive 6693 - Flow Game
- uvalive 6693 - Flow Game - BFS
- UVALive 6693 - Flow Game(点在线段上判断)
- [UVALive 6693]Flow Game计算几何,线代相交
- UVALive 6833Miscalculation (模拟)
- uvalive 4254(二分+模拟)
- UVALive 7139 Rotation(模拟)
- UVALive 4264 Message (模拟)
- UVALive - 4264 Message (模拟)
- UVALive 3667 Ruler (模拟)
- UVALive 4222 Dance(模拟)
- Appearance Analysis UVALive(模拟)
- UVALive 7264 Kejin Game(最小割)
- UVALive 6089 Nine (暴力模拟)
- UVaLive/LA 6805 Pantun Grader(模拟)
- UVALive 6832Bit String Reordering (模拟)
- UVALive 7146 Defeat the Enemy(模拟)
- java io系列13之 BufferedOutputStream(缓冲输出流)的认知、源码和示例
- 线程的创建
- 菜单的状态与工具栏--孙鑫MFC第六课第二部分
- java中外部类修饰符和类成员修饰符总结--看了这你就不会再对修饰符感到迷茫
- 冒泡排序1
- UVAlive 6693 Flow Game(模拟)
- java--css(1)利用css给html网页换套装
- 过滤器、拦截器、监听器的区别
- 逆向 牛博士 授课小计
- java io系列14之 DataInputStream(数据输入流)的认知、源码和示例
- HDU2028Lowest Common Multiple Plus
- eclipse : updating maven dependencies 一直加载的解决办法
- Jsp页面内置对象不提示的问题解决
- iOS获取系统相册、相机、麦克风权限的工具