Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <cstring>
- #include <bitset>
- using namespace std;
- struct instr
- {
- bool free;int id;
- instr(bool free=false,int id=0): free(free),id(id){}
- };
- vector<int> occup;
- vector<instr> ainstr,binstr;
- bool used[1000][1000];
- int n,m,k;
- bool isbad(int a,int b)
- {
- if(a==n||b==m) return false;
- if(used[a][b]) return false;
- used[a][b]=true;
- bool any=false;
- if(ainstr[a].free)
- {
- occup[ainstr[a].id]=false;
- if(isbad(a+1,b))
- return true;
- occup[ainstr[a].id]=true;
- return false;
- }
- else if(binstr[b].free)
- {
- occup[binstr[b].id]=false;
- if(isbad(a,b+1))
- return true;
- occup[binstr[b].id]=true;
- return false;
- }
- else
- {
- if(occup[ainstr[a].id]==false)
- {
- any=true;
- occup[ainstr[a].id]=true;
- if(isbad(a+1,b))
- return true;
- occup[ainstr[a].id]=false;
- }
- if(occup[binstr[b].id]==false)
- {
- any=true;
- occup[binstr[b].id]=true;
- if(isbad(a,b+1))
- return true;
- occup[binstr[b].id]=false;
- }
- return !any;
- }
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- char op[2];
- int x;
- scanf("%d%d%d",&n,&m,&k);
- occup.clear();
- occup.resize(k,0);
- ainstr.clear();
- binstr.clear();
- for(int i=0;i<n;i++)
- {
- scanf("%s%d",op,&x);
- ainstr.push_back(instr(op[0]=='-',x-1));
- }
- for(int i=0;i<n;i++)
- {
- scanf("%s%d",op,&x);
- binstr.push_back(instr(op[0]=='-',x-1));
- }
- for(int i=0;i<n;i++)
- for(int j=0;j<m;j++)
- used[i][j]=false;
- if(isbad(0,0))
- printf(":-(\n");
- else
- printf(":-)\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment