knightL

newD

Sep 18th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <bitset>
  6. using namespace std;
  7.  
  8. struct instr
  9. {
  10.     bool free;int id;
  11.     instr(bool free=false,int id=0): free(free),id(id){}
  12. };
  13.  
  14. vector<int> occup;
  15. vector<instr> ainstr,binstr;
  16. bool used[1000][1000];
  17. int n,m,k;
  18.  
  19. bool isbad(int a,int b)
  20. {
  21.     if(a==n||b==m) return false;
  22.     if(used[a][b]) return false;
  23.     used[a][b]=true;
  24.     bool any=false;
  25.     if(ainstr[a].free)
  26.     {
  27.         occup[ainstr[a].id]=false;
  28.         if(isbad(a+1,b))
  29.             return true;
  30.         occup[ainstr[a].id]=true;
  31.         return false;
  32.     }
  33.     else if(binstr[b].free)
  34.     {
  35.         occup[binstr[b].id]=false;
  36.         if(isbad(a,b+1))
  37.             return true;
  38.         occup[binstr[b].id]=true;
  39.         return false;
  40.     }
  41.     else
  42.     {
  43.         if(occup[ainstr[a].id]==false)
  44.         {
  45.             any=true;
  46.             occup[ainstr[a].id]=true;
  47.             if(isbad(a+1,b))
  48.                 return true;
  49.             occup[ainstr[a].id]=false;
  50.         }
  51.         if(occup[binstr[b].id]==false)
  52.         {
  53.             any=true;
  54.             occup[binstr[b].id]=true;
  55.             if(isbad(a,b+1))
  56.                 return true;
  57.             occup[binstr[b].id]=false;
  58.         }
  59.         return !any;
  60.     }
  61.  
  62. }
  63.  
  64. int main()
  65. {
  66.     int T;
  67.     scanf("%d",&T);
  68.     while(T--)
  69.     {
  70.         char op[2];
  71.         int x;
  72.         scanf("%d%d%d",&n,&m,&k);
  73.         occup.clear();
  74.         occup.resize(k,0);
  75.         ainstr.clear();
  76.         binstr.clear();
  77.         for(int i=0;i<n;i++)
  78.         {
  79.             scanf("%s%d",op,&x);
  80.             ainstr.push_back(instr(op[0]=='-',x-1));
  81.         }
  82.         for(int i=0;i<n;i++)
  83.         {
  84.             scanf("%s%d",op,&x);
  85.             binstr.push_back(instr(op[0]=='-',x-1));
  86.         }
  87.         for(int i=0;i<n;i++)
  88.             for(int j=0;j<m;j++)
  89.                 used[i][j]=false;
  90.         if(isbad(0,0))
  91.             printf(":-(\n");
  92.         else
  93.             printf(":-)\n");
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment