Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- int n, m;
- int mat[6][6];
- int one[6][6];
- int cnt;
- short dp[4][5][1 << 20];
- short rec(int i, int j, int bitmask, int skok) {
- if(__builtin_popcount(bitmask) == cnt) {
- return 1;
- }
- if(dp[i][j][bitmask] != -1) {
- return dp[i][j][bitmask];
- }
- short result = 0;
- if(skok == 0) {
- for(int k = 0; k < n; k++) {
- if(k != i and !(bitmask & (1 << one[k][j])) and mat[k][j] == 1) {
- int new_mask = (bitmask | (1 << one[k][j]));
- result = max(result, rec(k, j, new_mask, 1));
- }
- }
- }
- else {
- for(int k = 0; k < m; k++) {
- if(k != j and !(bitmask & (1 << one[i][k])) and mat[i][k] == 1) {
- int new_mask = (bitmask | (1 << one[i][k]));
- result = max(result, rec(i, k, new_mask, 0));
- }
- }
- }
- return dp[i][j][bitmask] = result;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin >> n >> m;
- cnt = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 1) {
- one[i][j] = cnt;
- cnt++;
- }
- }
- }
- memset(dp, -1, sizeof dp);
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(mat[i][j] == 1) {
- if(rec(i, j, (1 << one[i][j]), 1) == true) {
- cout << "DA" << endl;
- return 0;
- }
- }
- }
- }
- cout << "NE" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement