UVaLive 6693 Flow Game (计算几何,线段相交)
admin
2024-02-07 06:10:55

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

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

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

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;typedef long long LL;
typedef pair 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

v1; vector

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; }

相关内容

热门资讯

国庆黄金周景区情况:大同古城半... 文| 芙昕 编辑 | 芙昕 国庆长假,很多人都计划着出门走走,可一到了那些热门景点,看到的往往不是山...
来大东北一共分两步:先“冷藏”... 还在被“东北=冰窖”的刻板印象吓退? 南方的“小土豆”们 别急着裹紧小棉袄 这个冬天 让“气候缓冲带...
第三届“长城之约”活动在河北涞... 11月15日,第三届"长城之约"全球推广活动暨世界文化遗产对话15日在河北省保定市涞源县启幕。 本次...
巴厘岛:时光在此停驻 (自由行... 曾几何时,世人只知巴厘岛而不知印尼。巴厘岛的美太过耀眼,以至于人们常常忘记——它只是印尼万千岛屿中最...