NC222439. [USACOFeb2021S]ComfortableCows
描述
输入描述
The first line contains a single integer N. Each of the next N lines contains two space-separated integers, indicating the (x,y) coordinates of a cow's cell.
输出描述
The minimum number of cows Farmer Nhoj needs to add for each i in 1…N, on N separate lines.
示例1
输入:
9 0 1 1 0 1 1 1 2 2 1 2 2 3 1 3 2 4 1
输出:
0 0 0 1 0 0 1 2 4
说明:
For i=4, Farmer Nhoj must add an additional cow at (2,1) to make the cow at (1,1) uncomfortable.
For i=9, the best Farmer Nhoj can do is place additional cows at (2,0), (3,0), (2,−1), and (2,3).
Problem credits: Benjamin Qi
C++ 解法, 执行用时: 184ms, 内存消耗: 6756K, 提交时间: 2021-07-01 20:22:44
#include <bits/stdc++.h> using namespace std; int dx[5]={0,1,0,-1,0}; int dy[5]={0,0,1,0,-1}; int m[3005][3005]; int jsq; int n; void dfs(int x,int y) { int flg=0; int ta1,ta2; if(!m[x][y]) return ; for(int i=1;i<=4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; // if(xx<0||xx>n||yy<n||yy>n) // continue; if(m[xx][yy]) flg++; else ta1=xx,ta2=yy; } if(flg!=3) return ; else { m[ta1][ta2]=1; jsq++; for(int i=0;i<=4;i++) dfs(ta1+dx[i],ta2+dy[i]); } } int main() { cin>>n; while(n--) { int x,y; cin>>x; cin>>y; x+=1000; y+=1000; if(m[x][y]) jsq--; m[x][y]=1; for(int i=0;i<=4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; dfs(xx,yy); } cout<<jsq<<endl; } return 0; }