読者です 読者をやめる 読者になる 読者になる

Tamflexの貯蔵庫

やる気のない備忘録

codeIQ 2749

contest contest-codeIQ language language-c++


オセロのAIと同じ要領でできる.
初手判定に要注意

int a[25];

int dx[] = {1,1, 1, 0,-1,-1,-1,0};
int dy[] = {1,0,-1,-1,-1, 0, 1,1};

vector<string> split(const string &str, char delim)
{
  istringstream iss(str); string tmp; vector<string> res;
  while(getline(iss, tmp, delim)) res.push_back(tmp);
  return res;
}

bool isOutOfBound(int x, int y)
{
  return x<0 || x>4 || y<0 || y>4;
}

bool canFlip(int p,int pos)
{
  if(a[pos]!=0) return false;
  REP(i,8)
  {
    int x = pos/5+dx[i];
    int y = pos%5+dy[i];
    if(isOutOfBound(x,y)) continue;
    if(a[x*5+y]==0 || a[x*5+y]==p) continue;
    while(1)
    {
      x += dx[i];
      y += dy[i];
      if(isOutOfBound(x,y)) break;
      if(a[x*5+y]==0) break;
      if(a[x*5+y]==p) return true;
    }
  }
  return false;
}

bool canPos(int p, int pos)
{
  if(a[pos]!=0) return false;
  REP(i,8)
  {
    int x = pos/5+dx[i];
    int y = pos%5+dy[i];
    if(isOutOfBound(x,y)) continue;
    if(a[x*5+y]==0 || a[x*5+y]==p) continue;
    while(1)
    {
      x += dx[i];
      y += dy[i];
      if(isOutOfBound(x,y)) break;
      if(a[x*5+y]==p) break;
      if(a[x*5+y]==0) return true;
    }
  }
  return false;
}

bool isAdj(int pos)
{
  if(a[pos]!=0) return false;
  REP(i,8)
  {
    int x = pos/5+dx[i];
    int y = pos%5+dy[i];
    if(isOutOfBound(x,y)) continue;
    if(a[x*5+y]) return true;
  }
  return false;
}

bool isInit(void)
{
  REP(i,5) REP(j,5) if(a[i*5+j]) return false;
  return true;
}

vector<int> moves(int p)
{
  vector<int> v;
  REP(i,25) if(canFlip(p,i)) v.push_back(i);
  if(v.size()>0) return v;
  REP(i,25) if(canPos(p,i)) v.push_back(i);
  if(v.size()>0) return v;
  REP(i,25) if(isAdj(i)) v.push_back(i);
  return v;
}

int main()
{
  char c[] = {' ','R','B','W','G'};
  FOR(i,1,4)
  {
    string s; cin >> s;
    auto t = split(s,',');
    if(s.size()<1) continue;
    FOR(j,1,t.size()-1) a[atoi(t[j].c_str())-1] = i;
  }
  bool f = isInit();
  FOR(i,1,4)
  {
    printf("%c",c[i]);
    if(f) printf(",13");
    else
    {
      auto v = moves(i);
      for(auto j : v) printf(",%d",j+1);
    }
    printf("\n");
  }
}