UVA 10401---Injured Queen Problem+DP

题目链接:点击进入

定义dp[i][j]表示将一个皇后放在第i列第j行有多少种放法.则转移方程dp[i][j]+=dp[i-1][k].如果flag[i]==’?’,1<=j<=n,否则j只能取一个值,而1<=k<=j-2,j+2<=k<=n.

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

#define maxn 20
long long dp[maxn][maxn];
char flag[maxn];

void Init(int n)
{
      if(flag[1]==‘?‘)
      {
          for(int i=1;i<=n;i++)
              dp[1][i]=1;
      }
      else
    {
          int j;
          if(flag[1]>=‘A‘&&flag[1]<=‘Z‘)
                j=flag[1]-‘A‘+10;
          else
               j=flag[1]-‘0‘;
          dp[1][j]=1;
    }
}

int main()
{
       //freopen("in.txt","r",stdin);
       while(scanf("%s",flag+1)!=EOF)
       {
             int n=strlen(flag+1);
             memset(dp,0,sizeof(dp));
             Init(n);
             for(int i=2;i<=n;i++)
             {
                  if(flag[i]==‘?‘)
                  {
                        for(int j=1;j<=n;j++)
                        { ///本次放在dp[i][k]位置上,上次放在dp[i-1][k]位置上
                               for(int k=1;k<j-1;k++)
                                   dp[i][j]+=dp[i-1][k];
                               for(int k=j+2;k<=n;k++)
                                   dp[i][j]+=dp[i-1][k];
                        }
                  }
                  else  ///如果本列只能固定放的话,那么就只能放在一个位置
                  {
                         int j;
                         if(flag[i]>=‘A‘&&flag[i]<=‘Z‘)
                              j=flag[i]-‘A‘+10;
                         else
                             j=flag[i]-‘0‘;
                        for(int k=1;k<j-1;k++)
                            dp[i][j]+=dp[i-1][k];
                        for(int k=j+2;k<=n;k++)
                            dp[i][j]+=dp[i-1][k];
                  }
            }
            long long ans=0;
            for(int i=1;i<=n;i++)
                 ans+=dp[n][i];
            printf("%lld\n",ans);
       }
   return 0;
}
时间: 2024-10-14 15:26:43

UVA 10401---Injured Queen Problem+DP的相关文章

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

Uva 10401 Injured Queen Problem ( 计数DP)

Problem I Injured Queen Problem Input: standard input Output: standard output Time Limit: 6 seconds Memory Limit: 32 MB Chess is a two-player board game believed to have been played in India as early as the sixth century. However, in this problem we

uva 11195 Another queen (用状态压缩解决N后问题)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2136 Problem A Another n-Queen Problem I guess the n-queen problem is known by every person who has studied backtracking. In this problem you s

UVA 14000 Lighting System Design(DP)

You are given the task to design a lighting system for a huge conference hall. After doing a lot of calculation & sketching, you have figured out the requirements for an energy-efficient design that can properly illuminate the entire hall. According

UVA 10003 Cutting Sticks(区间dp)

Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they onl

uva10401Injured Queen Problem(递推)

题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现状,???3?4:在一个6*6的棋盘上,由于皇后是能够列向攻击的,所以一列仅仅能放一个皇后,所以第一个?代表第一列的皇后放的行未知,这样3的意思就是第4列皇后在第三行.也就是确定了第4列皇后的位置. 要求棋盘上的皇后不互相攻击.问这种棋盘能够有多少不同的放法. 解题思路:一開始想状态.想把不同的棋盘

Uva 10817 Headmaster&#39;s Headache (DP+ 状态压缩)

Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or

UVA - 1323 Vivian&#39;s Problem

Description The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliestdocumented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivatedby r

UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。

/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数. 思路:数位dp: dp[leadzero][i][j][k]表示前面是否选过非0数,即i长度之后可以第一个出现0,而不是前导0,长度为i,前面出现j,k次,j出现的次数. */ #include<iostream> #include<cstri