Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 6229
Reference question: 78452558
Https://www.cnblogs.com/cxhscst2/p/8215717.html
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define mst(a,b) memset((a),(b),sizeof(a)) 6 #define mp(a,b) make_pair(a,b) 7 #define pi acos(-1) 8 #define pii pair<int,int> 9 #define pb push_back10 const int INF = 0x3f3f3f3f;11 const double eps = 1e-6;12 const int MAXN = 15e4 + 10;13 const int MAXM = 2e6 + 10;14 15 int n,k;16 int dx[4] = {-1,0,0,1};17 int dy[4] = {0,-1,1,0};18 map<pii,bool>ma;19 map<pii,int>sub;20 21 bool judge(int x,int y) {22 if(x < 0 || x >= n || y < 0 || y >= n) return false;23 return true;24 }25 26 int check(int x,int y) {27 if(x == 0 || x == n - 1) {28 if(y == 0 || y == n - 1) return 3;29 else return 4;30 } else {31 if(y == 0 || y == n - 1) return 4;32 else return 5;33 }34 }35 36 int main() {37 #ifdef local38 freopen("data.txt", "r", stdin);39 #endif40 int cas = 1;41 int t;42 scanf("%d",&t);43 while(t--) {44 ma.clear();45 sub.clear();46 scanf("%d%d",&n,&k);47 if(n == 1) {48 printf("Case #%d: 1/1\n",cas++);49 continue;50 }51 ll ans1 = 16ll * (n - 2) + 5ll * (n - 2) * (n - 2) + 12;52 ll ans2 = 5ll * (1ll * n * (n + 1) / 2 - 2ll * (n - 2) - 3) + 8ll * (n - 2) + 9;53 while(k--) {54 int x,y;55 scanf("%d%d",&x,&y);56 if(ma[mp(x,y)]) {57 ans1 -= sub[mp(x,y)];58 if(x + y >= n - 1) ans2 -= sub[mp(x,y)];59 sub[mp(x,y)] = 0;60 } else {61 ma[mp(x,y)] = true;62 ans1 -= check(x,y);63 if(x + y >= n - 1) ans2 -= check(x,y);64 sub[mp(x,y)] = 0;65 }66 for(int i = 0; i < 4; i++) {67 int nx = x + dx[i], ny = y + dy[i];68 if(!judge(nx,ny)) continue;69 if(ma[mp(nx,ny)]) {70 if(sub[mp(nx,ny)]) {71 ans1--;72 if(nx + ny >= n - 1) ans2--;73 sub[mp(nx,ny)]--;74 }75 } else {76 ma[mp(nx,ny)] = true;77 sub[mp(nx,ny)] = check(nx,ny) - 1;78 ans1--;79 if(nx + ny >= n - 1) ans2--;80 }81 }82 }83 ll gcd = __gcd(ans1,ans2);84 ans1 /= gcd, ans2 /= gcd;85 printf("Case #%d: %lld/%lld\n",cas++,ans2,ans1);86 }87 return 0;88 }
ACM-ICPC 2017 Shenyang Division Field Competition M. Wandering robots & HDU 6229