코딩도사의 코드정리

Little Bishops 본문

컴퓨터 이야기/C++

Little Bishops

코딩도사 2011. 1. 8. 01:57
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <limits>
using namespace std;


#ifdef mycom
ifstream fin("input");
#else
#define fin cin
#endif
char chess[100][100] = {0,};
int n, k;
long long total = 0;
bool possible(int y, int x)
{
 int newy = y;
 int newx = x;
 for( ; newy >= 0 && newx >= 0; newy--, newx--)
 {
  if(chess[newy][newx] == true)
  {
   return false;
  }
 }
 newy = y;
 newx = x;
 for( ; newy < n && newx < n; newy++, newx++)
 {
  if(chess[newy][newx] == true)
  {
   return false;
  }
 }
 newy = y;
 newx = x;
 for( ; newy < n && newx >= 0; newy++, newx--)
 {
  if(chess[newy][newx] == true)
  {
   return false;
  }
 }
 newy = y;
 newx = x;
 for( ; newy >= 0 && newx < n; newy--, newx++)
 {
  if(chess[newy][newx] == true)
  {
   return false;
  }
 }
 return true;
}

inline bool sizecompare(int _y, int _x, int __y, int __x)
{
 //_ 가 크면 true;
 if(_y != __y)
  return _y > __y;
 else if(_y == __y)
 {
  return _x > __x;
 }
}
void back(int selcount, int lasty, int lastx)
{
 if(selcount == k)
 {
  total++;
 }
 else
 {
  for(int y=0; y<n; y++)
  {
   for(int x=0; x<n; x++)
   {    
    if(sizecompare(y, x, lasty, lastx) && possible(y, x))
    {
     chess[y][x] = true;
     back(selcount+1, y, x);
     chess[y][x] = false;
    }
   }
  }
 }
}

int main()
{
 bool bfirst = true;
 while(fin >> n >> k && n != 0 && k != 0)
 {
  if(bfirst != true)
  {
   cout << endl;
  }
  bfirst = false;
  total = 0;
  back(0, -1, 0);
  cout << total;
 }
 return 0;
}


'컴퓨터 이야기 > C++' 카테고리의 다른 글

strlen 따라잡기 in KLDP.ORG  (0) 2011.01.13
Queue  (0) 2011.01.08
순열  (0) 2011.01.07
부분집합  (0) 2011.01.07
soccer UVA  (0) 2011.01.07