Skip to content

Instantly share code, notes, and snippets.

@swimmi
Created April 21, 2014 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swimmi/11133595 to your computer and use it in GitHub Desktop.
Save swimmi/11133595 to your computer and use it in GitHub Desktop.
Is Tom's meadow beautiful now?
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//Is Tom's meadow beautiful now?
int main()
{
//ifstream cin("beautiful_meadow.txt");
int n,m;
while(cin>>n>>m)
{
bool b=true;
if(n==0) break;
vector<int>v; //mowed squares
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int c;
cin>>c;
if(c==0) v.push_back(i*10+j); //1<<n,m<<10
}
}
if(v.size()==0) //no mowed squares
b=false;
if(v.size()>1) //more than one mowed squares
{
vector<int>::iterator it;
for(int i=0;i<v.size();i++)
{
int a[4]={v[i]-1,v[i]+1,v[i]+10,v[i]-10}; //adjacent squares
for(int j=0;j<4;j++)
{
it=find(v.begin(),v.end(),a[j]);
if(*it>0) //no two squares are adjacent
{
b=false;
break;
}
}
if(!b) break;
}
}
if(b) cout<<"Yes"<<endl; //beautiful
if(!b) cout<<"No"<<endl; //not beautiful
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment