题意:给个棋盘,你可以在棋盘的边缘处放2个蓝色棋子2个黄色棋子,问连接2组同色棋子的最小代价,如果线路交叉,输-1。

析:交叉么,可以把它们看成是两条线段,然后如果相交就是不行的,但是有几种特殊情况,比如都在同一行或同一列,要特殊考虑这种情况。

1122,1212,2211,2121,1221,2112.这是几种特殊的,然后其他的就可以用判交叉来算了,然后最短路就是横纵坐标相减的绝对值加2.

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){ return a < b ? b : a; }
inline LL Min(LL a, LL b){ return a > b ? b : a; }
inline int Max(int a, int b){ return a < b ? b : a; }
inline int Min(int a, int b){ return a > b ? b : a; }
char s[15][15];
vector<P> v1;
vector<P> v2; inline int mult(const P &a, const P &b, const P &c){
return (a.first-c.first)*(b.second-c.second) - (b.first-c.first)*(a.second-c.second);
} bool intersection(const P &aa, const P &bb, const P &cc, const P &dd){
if(Max(aa.first, bb.first) < Min(cc.first, dd.first)) return false;
if(Max(aa.second, bb.second) < Min(cc.second, dd.second)) return false;
if(Max(cc.first, dd.first) < Min(aa.first, bb.first)) return false;
if(Max(cc.second, dd.second) < Min(aa.second, bb.second)) return false;
if(mult(cc, bb, aa) * mult(bb, dd, aa) < 0) return false;
if(mult(aa, dd, cc) * mult(dd, bb, cc) < 0) return false;
return true;
} bool solve(int a, int b, int c, int d){
if(a < b && b < c && c < d){ printf("%d\n", b-a+d-c+2); return true; }
else if(c < d && d < a && a < b){ printf("%d\n", b-a+d-c+2); return true; }
else if(a < c && c < d && d < b){ printf("%d\n", b-a+d-c+4); return true; }
else if(c < a && a < b && b < d){ printf("%d\n", b-a+d-c+4); return true; }
else if(a < c && c < b && b < d){ printf("-1\n"); return true; }
else if(c < a && a < d && d < b){ printf("-1\n"); return true; } return false;
} bool judge1(){
bool ok = true;
if(v1[0].first == v1[1].first && v1[1].first == v2[0].first && v1[1].first == v2[1].first){
if(solve(v1[0].second, v1[1].second, v2[0].second, v2[1].second)) return true;
}
else if(v1[0].second == v1[1].second && v1[1].second == v2[0].second && v1[1].second == v2[1].second){
if(solve(v1[0].first, v1[1].first, v2[0].first, v2[1].first)) return true;
}
return false;
} int main(){
int T; cin >> T;
while(T--){
v1.clear(); v2.clear();
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%s", s[i]+1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(s[i][j] == '1') v1.push_back(P(i, j));
else if(s[i][j] == '2') v2.push_back(P(i, j));
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
if(judge1()) continue;
if(intersection(v1[0], v1[1], v2[0], v2[1])){ printf("-1\n"); continue; }
printf("%d\n", abs(v1[1].first-v1[0].first)+abs(v1[1].second-v1[0].second)+abs(v2[1].first-v2[0].first)+abs(v2[1].second-v2[0].second)+2);
}
return 0;
}

UVaLive 6693 Flow Game (计算几何,线段相交)的更多相关文章

  1. POJ2284 That Nice Euler Circuit (欧拉公式)(计算几何 线段相交问题)

                                                          That Nice Euler Circuit Time Limit: 3000MS   M ...

  2. POJ 3347 Kadj Squares (计算几何+线段相交)

    题意:从左至右给你n个正方形的边长,接着这些正方形都按照旋转45度以一角为底放置坐标轴上,最左边的正方形左端点抵住y轴,后面的正方形依次紧贴前面所有正方形放置,问从上方向下看去,有哪些正方形是可以被看 ...

  3. poj1410计算几何线段相交

    You are to write a program that has to decide whether a given line segment intersects a given rectan ...

  4. zoj 1010 Area【线段相交问题】

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...

  5. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  6. 【计算几何初步-代码好看了点线段相交】【HDU2150】Pipe

    题目没什么 只是线段相交稍微写的好看了点 Pipe Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. 【计算几何初步-线段相交】【HDU1089】线段交点

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. 51nod_1264:线段相交(计算几何)

    题目链接 关于判断线段相交,具体算法见 点击打开链接 ,先进行快速排斥试验,若不能判断出两个线段不相交,再进行跨立试验. //吐槽1,long long 会溢出... //吐槽2,只进行跨立试验的虽然 ...

  9. (计算几何 线段判交) 51nod1264 线段相交

    1264 线段相交 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交). 如果相交,输出"Yes",否则输出"No".   ...

随机推荐

  1. Sqoop的使用(Mysql To HBase)

    最近需要将mysql的数据整合到HBase中,原本使用MapReduce,自己制作job将mysql的数据导入, 查阅资料过程中,发现了开源工具sqoop(关系性数据库与HDFS,HBASE,HIVE ...

  2. 手动编译Spring4.2源码,以及把源码导入myEclipse中

    前提条件,先在本地配置好JDK1.8, 安装好Git 执行下面命令: 1,双击Git Bash, 进入源码存放路径 cd E:\Foler 2,git clone git@github.com:spr ...

  3. JBPM4入门——6.流程实例的创建和执行

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

  4. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.4.无法图形化安装Grid Infrastructure

    无法图形化安装: [grid@linuxrac1 grid]$ ./runInstaller Starting Oracle Universal Installer... Checking Temp ...

  5. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.2.Oracleasm Createdisk ASM磁盘失败:Instantiating disk: failed

    1.错误信息:Instantiating disk: failed [root@linuxrac1 /]# /usr/sbin/oracleasm createdisk OCR_VOTE /dev/s ...

  6. library cache lock和cursor: pin S wait on X等待

    1.现象: 客户10.2.0.4 RAC环境,出现大量的library cache lock和cursor: pin S wait on X等待,经分析是由于统计信息收集僵死导致的.数据库在8点到9点 ...

  7. [转] C#操作Excel文件

    来自  jbp74c37ad170 的文章EXCEL编程语句有那些啊 全面控制 Excel首先创建 Excel 对象,使用ComObj:Dim ExcelID as Excel.Application ...

  8. Effective java笔记6--异常

    充分发挥异常的优点,可以提高一个程序的可读性.可靠性和可维护性.如果使用不当的话,它们也会带来负面影响. 一.只针对不正常的条件才使用异常 先看一段代码: //Horrible abuse of ex ...

  9. Delphi 7使用自定义图标关联文件类型

    Delphi 7使用自定义图标关联文件类型 5.2 Delphi编程(40)  版权声明:本文为博主原创文章,未经博主允许不得转载. 在开发过程中,我们经常需要属于自己的文件类型,自定义的后缀名不仅可 ...

  10. MSDN 中 对vector::erase()的解释.xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...