zoj1002
#include <vector>
#include <iostream>
using namespace std;
int length,ops;
vector<vector<int> > city;
int openCount(int x,int y)
{
int co=0;
if(x>0&&city[x-1][y]!=0)co++;
if(y>0&&city[x][y-1]!=0)co++;
if(x<length-1&&city[x+1][y]!=0)co++;
if(y<length-1&&city[x][y+1]!=0)co++;
return co;
}
void setFlag(int x,int y)
{
for(int s=x-1;s>=0;s--)
{
if(city[s][y]==0)break;
else if(city[s][y]!=-2) {city[s][y]=-2;ops--;}
}
for(int s=y-1;s>=0;s--)
{
if(city[x][s]==0)break;
else if(city[x][s]!=-2) {city[x][s]=-2;ops--;}
}
for(int s=x+1;s<length;s++)
{
if(city[s][y]==0)break;
else if(city[s][y]!=-2) {city[s][y]=-2;ops--;}
}
for(int s=y+1;s<length;s++)
{
if(city[x][s]==0)break;
else if(city[x][s]!=-2) {city[x][s]=-2;ops--;}
}
}
int getFlag(int x,int y)
{
int co=0;
for(int s=x-1;s>=0;s--)
{
if(city[s][y]==0)break;
else co++;
}
for(int s=y-1;s>=0;s--)
{
if(city[x][s]==0)break;
else co++;
}
for(int s=x+1;s<length;s++)
{
if(city[s][y]==0)break;
else co++;
}
for(int s=y+1;s<length;s++)
{
if(city[x][s]==0)break;
else co++;
}
return co;
}
int main()
{
while(cin>>length&&length!=0)
{
char c;
ops=0;
if(length==1)
{
cin>>c;
if(c=='.')cout<<"1"<<endl;
else cout<<"0"<<endl;
}
else
{
city.clear();
city.resize(length,vector<int>(length));
int count=0;
int min_open,min_fl;
int opc=0,flag=0;
int x,y;
for(int i=0;i<length;i++)
for(int j=0;j<length;j++)
{
cin>>c;
if(c=='.'){city[i][j]=1;ops++;}
else city[i][j]=0;
}
while(ops!=0)
{
min_open=5;
min_fl=16;
for(int i=0;i<length;i++)
for(int j=0;j<length;j++)
{
if(city[i][j]==1)
{
opc=openCount(i,j);
flag=getFlag(i,j);
if((opc<min_open)||(opc==min_open&&flag<min_fl))
{min_fl=flag;min_open=opc;x=i;y=j;}
}
}
city[x][y]=-1;
ops--;
count++;
setFlag(x,y);
}
cout<<count<<endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int length,ops;
vector<vector<int> > city;
int openCount(int x,int y)
{
int co=0;
if(x>0&&city[x-1][y]!=0)co++;
if(y>0&&city[x][y-1]!=0)co++;
if(x<length-1&&city[x+1][y]!=0)co++;
if(y<length-1&&city[x][y+1]!=0)co++;
return co;
}
void setFlag(int x,int y)
{
for(int s=x-1;s>=0;s--)
{
if(city[s][y]==0)break;
else if(city[s][y]!=-2) {city[s][y]=-2;ops--;}
}
for(int s=y-1;s>=0;s--)
{
if(city[x][s]==0)break;
else if(city[x][s]!=-2) {city[x][s]=-2;ops--;}
}
for(int s=x+1;s<length;s++)
{
if(city[s][y]==0)break;
else if(city[s][y]!=-2) {city[s][y]=-2;ops--;}
}
for(int s=y+1;s<length;s++)
{
if(city[x][s]==0)break;
else if(city[x][s]!=-2) {city[x][s]=-2;ops--;}
}
}
int getFlag(int x,int y)
{
int co=0;
for(int s=x-1;s>=0;s--)
{
if(city[s][y]==0)break;
else co++;
}
for(int s=y-1;s>=0;s--)
{
if(city[x][s]==0)break;
else co++;
}
for(int s=x+1;s<length;s++)
{
if(city[s][y]==0)break;
else co++;
}
for(int s=y+1;s<length;s++)
{
if(city[x][s]==0)break;
else co++;
}
return co;
}
int main()
{
while(cin>>length&&length!=0)
{
char c;
ops=0;
if(length==1)
{
cin>>c;
if(c=='.')cout<<"1"<<endl;
else cout<<"0"<<endl;
}
else
{
city.clear();
city.resize(length,vector<int>(length));
int count=0;
int min_open,min_fl;
int opc=0,flag=0;
int x,y;
for(int i=0;i<length;i++)
for(int j=0;j<length;j++)
{
cin>>c;
if(c=='.'){city[i][j]=1;ops++;}
else city[i][j]=0;
}
while(ops!=0)
{
min_open=5;
min_fl=16;
for(int i=0;i<length;i++)
for(int j=0;j<length;j++)
{
if(city[i][j]==1)
{
opc=openCount(i,j);
flag=getFlag(i,j);
if((opc<min_open)||(opc==min_open&&flag<min_fl))
{min_fl=flag;min_open=opc;x=i;y=j;}
}
}
city[x][y]=-1;
ops--;
count++;
setFlag(x,y);
}
cout<<count<<endl;
}
}
return 0;
}
虽然终于AC过去了,但其实还是失败的,因为我都不能证明自己的算法是正确的...大学时学得真是忘得差不多了,要好好补补算法的基础知识了~~下次写个心中有数的算法再AC一次!
I'm back!