Why D WA。。。
#include<bits/stdc++.h> #define endl '\n' using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; void solve(){ ll n,m,q;cin>>n>>m>>q; string s=""; for(int i=0;i<n;i++){ string line; cin>>line; s += line; } vector<ll> a(q+1); a[0] = bitset<63>(s).to_ullong(); for(int i=1;i<=q;i++){ s = ""; for(int j=0;j<n;j++){ string line; cin>>line; s+=line; } a[i] = bitset<63>(s).to_ullong(); } map<ll,ll> mp[2]; mp[0][a[0]] = 0; map<ll,ll> path,idx; //path记录转移前的状态,x通过a[i]转移到x|a[i] path[x|a[i]] = x; //idx 记录用哪个计划转移 idx[x|a[i]] = i; path[a[0]] = 0; idx[a[0]] = 0; for(ll i=1;i<=q;i++){ mp[i&1] = mp[(i-1)&1]; for(auto &[x,y]:mp[(i-1)&1]){ if(mp[i&1].find(x|a[i])==mp[i&1].end()){//之前的计划不能组合出x|a[i]状态 mp[i&1][x|a[i]] = y+1; path[x|a[i]] = x; idx[x|a[i]] = i; }else if(mp[i&1][x|a[i]]> y + 1){ mp[i&1][x|a[i]] = y+1; path[x|a[i]] = x; idx[x|a[i]] = i; } } } if(mp[q&1].find((1ll<<(n*m))-1)==mp[q&1].end()){ return cout<<"-1",void(); } cout<<mp[q&1][(1ll<<(n*m))-1]<<endl; ll res = idx[(1ll<<(n*m))-1]; ll state = path[(1ll<<(n*m))-1]; while(res!=0){ cout<<res<<endl; res = idx[state];state = path[state]; //if(res==0) break; } } int main(){ ifstream test_file("in.txt"); if (test_file) { freopen("in.txt", "r", stdin); freopen("output.txt", "w", stdout); } std::ios::sync_with_stdio(0);std::cout.tie(0);std::cin.tie(0); int T = 1; #ifdef MULTI_TEST cin>>T; #endif while(T--){ solve(); } return 0; }
WA 第4、5个点