You are viewing a single comment's thread. Return to all comments →
my solution with c++
#include <bits/stdc++.h> using namespace std; #define speed std::ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define sz size() #define pb push_back #define all(v) v.begin(),v.end() #define sm(v) accumulate(all(v),0ll) #define cin(v) for(auto &i : v) cin>>i vector<string>g(10),words; vector<int>vis(10); bool done=0; void solve(int i ){ if(sm(vis)==words.sz && !done) {done=1;for(auto a:g) cout<<a<<"\n";} if(i==10) return ; for(int j=0;j<10;j++){ if(g[i][j]=='+') continue; for(int k=0;k<words.sz;k++){ if(vis[k]) continue; vis[k]=1; vector<int>added;int c=0; for(int m=0;m<words[k].sz;m++) { int ind=m+j; if(ind>=10 || g[i][ind] =='+') break; if(g[i][ind] =='-') g[i][ind]=words[k][m],added.pb(ind); else if(g[i][ind]!=words[k][m]) break; else c++; } if(added.sz+c==words[k].sz) solve(i); for(auto ind:added) { g[i][ind]='-'; } added.clear();c=0; for(int m=0;m<words[k].sz;m++) { int ind=m+i; if(ind>=10 || g[ind][j] =='+') break; if(g[ind][j] =='-') g[ind][j]=words[k][m],added.pb(ind); else if(g[ind][j]!=words[k][m]) break; else c++; } if(added.sz+c==words[k].sz) solve(i); for(auto ind:added) { g[ind][j]='-'; } vis[k]=0; } } solve(i+1); } void runCase(){ cin(g); string s,x;cin>>s; for(auto a:s){ if(a==';') {words.pb(x);x="";} else x+=a; } words.pb(x); solve(0); } int main(){speed; runCase(); }
Crossword Puzzle
You are viewing a single comment's thread. Return to all comments →
my solution with c++