第十三次CCF计算机软件能力认证CSP题解

[**CSP13-1 跳一跳**](http://118.190.20.162/view.page?gpid=T73)
/*
csp13-1 跳一跳
2017/04/02
by Tawn
*/
#include 

using namespace std;
const int maxn = 100;

int main()
{
  int i;
  int j = 0;
  int s = 0;
  while(cin >> i && i)
  {
    if(i == 1) {s += 1; j = 0;}
    if(i == 2) {s += j+2; j += 2;}
  }
  cout << s << endl;
  return 0;
}

[**CSP13-2 碰撞的小球**](http://118.190.20.162/view.page?gpid=T72)
/*
csp13-2 碰撞的小球
2017/04/02
by Tawn
*/
#include 

using namespace std;
const int maxn = 100;

int n, L , t;
int pos[maxn];
int ori[maxn];

int main()
{
  cin >> n >> L >> t;
  for(int i = 1; i <= n; i++)
     {
       cin >> pos[i];
       ori[i] = 1;
     }

  while(t--)
  {
    for(int i = 1; i <= n; i++)
    {
      if(pos[i] == 0 || pos[i] == L) ori[i] = -ori[i];
      pos[i] += ori[i];
    }
    for(int i = 1; i <= n; i++)
      for(int j = i+1; j <= n; j++)
      {
        if(pos[i] == pos[j])
         {
           ori[i] = -ori[i];
           ori[j] = -ori[j];
         }
      }
  }
  for(int i = 1; i <= n; i++)
     {
       cout << pos[i];
       if(i < n) cout << " ";
       else cout << endl;
     }
  return 0;
}


[**CSP13-3 URL映射**](http://118.190.20.162/view.page?gpid=T71)

模拟大法好,参考了一下大佬的代码,理解能力很重要!

#include 

using namespace std;

#define pb(x)  push_back(x)
#define sc(x)  scanf("%d",&x);
#define pf(x)  printf("%d",x);
#define pfn    printf("\n");

vector rule;
map mp;
int n,m;

bool ismap(string &r,string &url)
{
   vector res;
   int ir = 0;
   int iu = 0;
   while(true)
   {
     if(iu >= url.size())   break;
     if(r[ir] == url[iu]) {iu ++,ir++;}
     else  if(r[ir] == '<')
      {
         ir ++;
         string s;
         switch(r[ir])
         {
           case 'p':
                cout << mp[r] ;
                for(int i = 0; i < res.size(); i++)
                  cout << " " << res[i];
           cout << " " << url.substr(iu) << endl;
           return true;

          case 'i':
              while(iu < url.size() && isdigit(url[iu]))
                s += url[iu++];
              if(s.empty()) return false;
              if(!(s.size() == 1 && s[0] == '0')) s = s.substr(s.find_first_not_of('0'));
              //cout << "* " << s << " *" << endl;
              res.pb(s);
              ir += 4;
              break;

          case 's':
             while(iu < url.size() && (isdigit(url[iu]) || isalpha(url[iu])))
                 s += url[iu++];
             if(s.empty()) return false;
             res.pb(s);
             ir += 4;
             break;
          default : break;
         }
       }
    else return false;
   }
   if(ir >= r.size())
   {
     cout << mp[r];
     for(int i = 0; i < res.size(); i++)
       cout << " " << res[i];
     cout << endl;
     return true;
   }
   return false;
}


void solve(string &url)
{
  for(int i = 0; i < n; i++)
   {
     if(ismap(rule[i],url)) return;
   }
  printf("404\n");
  return ;
}


int main()
{
  scanf("%d%d",&n,&m);
  for(int i = 0; i < n; i++)
  {
    string str1,str2;
    cin >> str1 >> str2;
    rule.pb(str1);
    mp[str1] = str2;
  }
  for(int i = 0; i < m; i++)
  {
    string url;
    cin >> url;
    solve(url);
  }
  return 0;
}

[**CSP13-4 棋局评估**](http://118.190.20.162/view.page?gpid=T70)
/*
csp13-4 棋力评估
2018/05/05
by Tawn
*/
#include 

using namespace std;

const int maxn = 1000;
const int INF = 0x3f3f3f3f;
map  cnt;


int check(int a[10])
{
  if(a[1] != 0 && a[1] == a[2] && a[2] == a[3]) return a[1];
  if(a[4] != 0 && a[4] == a[5] && a[5] == a[6]) return a[4];
  if(a[7] != 0 && a[7] == a[8] && a[8] == a[9]) return a[7];
  if(a[1] != 0 && a[1] == a[4] && a[4] == a[7]) return a[1];
  if(a[2] != 0 && a[2] == a[5] && a[5] == a[8]) return a[2];
  if(a[3] != 0 && a[3] == a[6] && a[6] == a[9]) return a[3];
  if(a[1] != 0 && a[1] == a[5] && a[5] == a[9]) return a[1];
  if(a[3] != 0 && a[3] == a[5] && a[5] == a[7]) return a[5];
 
  for(int i = 1; i <= 9; i++)
    if(!a[i]) return 0;
  return -1;
}

int dfs(int a[10], int t)
{
 
  string s;
  int ans;
  int num = 1;
  for(int i = 1; i <= 9; i++)
    {
      s += a[i] + '0';
      if(!a[i]) num++;
    }

  if(cnt.count(s) == 1) return cnt[s]; 
  
  int res = check(a);

  if(res == -1) {cnt[s] = 0; return 0;}
  if(res == 1)  {cnt[s] = num;return num;}
  if(res == 2)  {cnt[s] = -1*num; return -1*num;}
  

  if(t == 1) ans = -1*INF;
  else       ans = INF;

  for(int i = 1; i <= 9; i++)
  {
    if(a[i])  continue;
   
    a[i] = t;
    if(t == 1)
    {
      int ans1 = dfs(a,2);
      ans = max(ans,ans1);
    }
    else
    {
      int ans1 = dfs(a,1);
      ans = min(ans,ans1);
    }
    a[i] = 0;    
  }
  return ans;  
}

int main()
{
  int a[10];
  int T;
  cin >> T;
  while(T--)
  {
  for(int i = 1; i <= 9; i++)  cin >> a[i];
  int ans = dfs(a,1);
  cout << ans << endl;
  }
}

你可能感兴趣的:(比赛套题)