UVA 10401 injured Queen problem

Source: Internet
Author: User

Original question:
Chess is a Two-player board game believed to has been played in India as early as the sixth century. However, in this problem we won't discuss about chess, rather we'll talk about a modified form of the classic N-quee NS problem. I know you is familiar with plotting n-queens in a chess board with the help of a classic backtracking algorithm. If you write that algorithm
Now you'll find that there is ways of plotting 8 queens in a 8x8 board provided no Queens attack each other. In this problem we'll talk about injured queens who can move only like a king in horizontal and diagonal direction from Current position but can reach any row from the current position like a normal chess queen. You'll have to find the number of possible arrangements with such injured Queens in a particular (NXN) board (with some Additional constraints), such that no, and Queens attack each other.

Fig:injured Queen at A6 can reach the adjacent grey squares. Queen at E4 can reach adjacent grey
Squares. The injured Queen positions is black and the reachable places are grey.
Input
Input file contains several lines of input. Each line expresses a certain board status. The length of these
The status string is the Board dimension n (0 < n≤15). The first character of the string denotes the status
of first column, the second character of the string denotes the status of the the second column and so on.
So if the first character of the status string was 2, it means that we were looking for arrangements (no
Injured Queen attack each other) which have injured queen in column A, row 2. The possible numbers
For rows is,..., d,e,f which indicates row 1, 2, 3 ... 13, 14, 15. If any column contains '? '
It means that in that column the injured queen can is in any row. So a status string ' 1?4?? 3 means
That's asked to find out total number of possible arrangements in a (6x6) chessboard which
has three of it six injured Queens at A1, C4 and F3. Also Note that there'll be no invalid inputs. For
Example ' 1?51 ' is an invalid input because a (4×4) chessboard does not having a fifth row.
Output
For each line of input produce one line of output. This line should contain an integer which indicates
The total number of possible arrangements of the corresponding input status string.
Sample Input
??????
???????????????
??? 8?????
?????
Sample Output
2642
22696209911206174
2098208
0
Effect:
Give you a NXN board, and now let you put a wounded queen on the board. Each queen's attack range, as shown in the picture, now asks you how many placement methods a NXN-sized chessboard is. Give you a string that represents the state of each column, if the character is '? ' Indicates that the column is empty and can be placed casually, if a number indicates that the column already has an injured queen.

State compression code #include <bits/stdc++.h> using namespace std;
FStream in,out;
Long Long dp[16][32770];
string S;
    int Getnum (char c) {if (c>= ' 1 ' &&c<= ' 9 ') return C ' 0 ';
else return C ' A ' +10;
    } BOOL cmp (int a,int b) {if (a==b) return false;
    if (a<<1==b) return false;
    if (a>>1==b) return false;
return true;
    } int main () {Ios::sync_with_stdio (false);
    int n;
        while (cin>>s) {memset (dp,0,sizeof (DP));
        int len=s.size ();
            if (s[0]== '? ')
        for (int i=0;i<len;i++) dp[0][1<<i]=1;
        Else dp[0][1<< (Getnum (S[0])-1)]=1;
            for (int i=1;i<len;i++) {if (s[i]== '? ')
                    {for (int j=0,pj=1;j<len;++j,pj=1<<j) {if (s[i-1]== '? ')
         {for (int k=0,pk=1;k<len;++k,pk=1<<k)                   if (CMP (PJ,PK)) DP[I][PJ]+=DP[I-1][PK];
                        } else {int pk=1<< (Getnum (S[i-1])-1);
                    if (CMP (PJ,PK)) DP[I][PJ]+=DP[I-1][PK];
                }}} else {int pj=1<< (Getnum (S[i])-1);
                if (s[i-1]== '? ')
                            {for (int k=0,pk=1;k<len;++k,pk=1<<k) if (CMP (PJ,PK))
                DP[I][PJ]+=DP[I-1][PK];
                    } else {int pk=1<< (Getnum (S[i-1])-1);
                if (CMP (PJ,PK)) DP[I][PJ]+=DP[I-1][PK];
        }}} long long ans=0;
 for (int i=0,pi=1;i<len;++i,pi=1<<i) ANS+=DP[LEN-1][PI];       cout<<ans<<endl;
} return 0;
 }
Normal code #include <bits/stdc++.h> using namespace std;
FStream in,out;
Long Long dp[16][16];
string S;
    int Getnum (char c) {if (c>= ' 1 ' &&c<= ' 9 ') return C ' 0 ';
else return C ' A ' +10;
    } BOOL cmp (int a,int b) {if (a==b) return false;
    if (a+1==b) return false;
    if (a-1==b) return false;
return true;
    } int main () {Ios::sync_with_stdio (false);
    int n;
        while (cin>>s) {memset (dp,0,sizeof (DP));
        int len=s.size ();
            if (s[0]== '? ')
        for (int i=0;i<len;i++) dp[0][i]=1;
        else Dp[0][getnum (s[0]) -1]=1;
            for (int i=1;i<len;i++) {if (s[i]== '? ')
                    {for (int j=0;j<len;++j) {if (s[i-1]== '? ')
                                {for (int k=0;k<len;++k) if (CMP (J,K)) dp[i][j]+=dp[I-1][K];
                        } else {int k=getnum (s[i-1])-1;
                    if (CMP (J,K)) dp[i][j]+=dp[i-1][k];
                }}} else {int j=getnum (s[i])-1;
                if (s[i-1]== '? ') {for (int k=0;k<len;++k) if (CMP (J,K)) dp[i][j]+
                =DP[I-1][K];
                    } else {int k=getnum (s[i-1])-1;
                if (CMP (J,K)) dp[i][j]+=dp[i-1][k];
        }}} long long ans=0;
        for (int i=0;i<len;++i) ans+=dp[len-1][i];
    cout<<ans<<endl;
} return 0;
 }

Answer:
Recently just looked at the dynamic planning of state compression, see this problem to be solved by the method of state compression. But because a queen can attack an entire column, a column can only be a queen, so there is no state compression to represent each row. But it's good practice to get started with the problem of state compression. The
transfer equation is also very simple, dp[i][j]+=dp[i-1][k] indicates how many placement methods are placed on row j in column I, in which J and K are judged to be conflicting, that is, whether J and K can attack each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.