Untitled
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
/// #include<iostream> using namespace std; int n; int arr[20][20]; int arrC[200][2]; int visit[20][20]; int step=0; int ans=1000000; int dx[]= {-1,0,1,0}; int dy[]= {0,1,0,-1}; void reset(){ int h=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ visit[i][j]=0; } } } bool check (int v, int d){ int tempx = arrC[v][0]+dx[d]; int tempy = arrC[v][1]+dy[d]; while(tempx >=0 && tempx<n && tempy >=0&&tempy<n){ if(visit[tempx][tempy]==1){ return false; } tempx = tempx+dx[d]; tempy= tempy+dy[d]; } return true; } int setWires(int v, int d){ int l=0; int tempx = arrC[v][0]+dx[d]; int tempy = arrC[v][1]+dy[d]; while(tempx >=0 && tempx < n && tempy >=0 && tempy < n){ visit[tempx][tempy]=1- visit[tempx][tempy]; l++; tempx = tempx+dx[d]; tempy= tempy+dy[d]; } return l; } void Try(int v,int sum){ if(v==step){ if(sum<ans){ ans=sum; } return; } for(int i=0;i<4;i++){ if(check(v,i)){ sum+= setWires(v,i); Try(v+1, sum); sum-= setWires(v,i); } } } int main(){ //freopen("input.txt","r",stdin); int t; cin>>t; for(int test=1;test<=t;test++){ cin>>n; step=0; reset(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>arr[i][j]; if(arr[i][j]==1){ arrC[step][0]=i; arrC[step][1]=j; visit[i][j]=1; step++; } } } ans=1000000; Try(0,0); cout<<"#"<<test<<" "<<ans<<endl; } return 0; }
Editor is loading...