HDU 4121 Xiangqi 模擬題

題目:?http://acm.hdu.edu.cn/showproblem.php?pid=4121

首先對標題贊一個,非要叫 “Xiangqi” 而不是 ”中國象棋“ 或者 ”Chinese chess“ 。。

然后是題意:黑棋只剩下一個”將“了,紅棋各種 ”車” “馬” “炮“,判斷黑棋是不是死棋了。。。

對于一個會下中國象棋的選手簡直簡單啊,第一次手賤加腦殘WA一次,稍微一改就0ms AC了,模擬題沒什么可以講的,代碼中有詳細注釋。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <algorithm>
  4 
  5 char palace[11][10]; //棋盤
  6 
  7 //判斷(x, y)點有沒有棋子
  8 char has_chess(int x, int y)
  9 {
 10     if(x < 1 || x > 10 || y < 1 || y > 9)
 11         return 0;
 12     return palace[x][y];
 13 }
 14 
 15 //判斷(x, y)點是不是棋子c
 16 bool is_chess(int x, int y, char c)
 17 {
 18     return has_chess(x, y) == c;
 19 }
 20 
 21 //判斷(x, y)點的左邊直線上有沒有棋子c,有的話返回橫坐標
 22 int has_left(int x, int y, char c)
 23 {
 24     for(int i = 1; i < y; i++)
 25     {
 26         if(palace[x][i] == c)
 27             return i;
 28     }
 29     return 0;
 30 }
 31 
 32 //判斷(x, y)點的右邊直線上有沒有棋子c,有的話返回橫坐標
 33 int has_right(int x, int y, char c)
 34 {
 35     for(int i = 9; i > y; i--)
 36     {
 37         if(palace[x][i] == c)
 38             return i;
 39     }
 40     return 0;
 41 }
 42 
 43 //判斷(x, y)點的上邊直線上有沒有棋子c,有的話返回縱坐標
 44 int has_up(int x, int y, char c)
 45 {
 46     for(int i = 1; i < x; i++)
 47     {
 48         if(palace[i][y] == c)
 49             return i;
 50     }
 51     return 0;
 52 }
 53 
 54 //判斷(x, y)點的下邊直線上有沒有棋子c,有的話返回縱坐標
 55 int has_down(int x, int y, char c)
 56 {
 57     for(int i = 10; i > x; i--)
 58     {
 59         if(palace[i][y] == c)
 60             return i;
 61     }
 62     return 0;
 63 }
 64 
 65 //判斷(x1, y)到(x2, y)之間棋子的數量
 66 int cnt_chess_x(int x1, int x2, int y)
 67 {
 68     if(x1 > x2)
 69         std::swap(x1, x2);
 70     int cnt = 0;
 71     for(x1++; x1 < x2; x1++)
 72     {
 73         if(palace[x1][y])
 74             cnt++;
 75     }
 76     return cnt;
 77 }
 78 
 79 //判斷(x, y1)到(x, y2)之間棋子的數量
 80 int cnt_chess_y(int y1, int y2, int x)
 81 {
 82     if(y1 > y2)
 83         std::swap(y1, y2);
 84     int cnt = 0;
 85     for(y1++; y1 < y2; y1++)
 86     {
 87         if(palace[x][y1])
 88             cnt++;
 89     }
 90     return cnt;
 91 }
 92 
 93 //判斷能否被“帥”吃掉
 94 bool general(int x, int y)
 95 {
 96     int gx, gy;
 97     for(int i = 8; i <= 10; i++)
 98         for(int j = 4; j <= 6; j++)
 99             if(palace[i][j] == 'G')
100             {
101                 gx = i;
102                 gy = j;
103             }
104     if(gy != y)return 0;
105     return cnt_chess_x(gx, x, y) == 0;
106 }
107 
108 //判斷能否被“車”吃掉
109 bool chariot(int x, int y)
110 {
111     int tmp = has_left(x, y, 'R');
112     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0)
113         return 1;
114     tmp = has_right(x, y, 'R');
115     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0)
116         return 1;
117     tmp = has_up(x, y, 'R');
118     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0)
119         return 1;
120     tmp = has_down(x, y, 'R');
121     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0)
122         return 1;
123     return 0;
124 }
125 
126 //判斷能否被“馬”吃掉
127 bool horse(int x, int y)
128 {
129     return(is_chess(x-2, y-1, 'H') && !has_chess(x-1, y-1)
130         || is_chess(x-2, y+1, 'H') && !has_chess(x-1, y+1)
131         || is_chess(x+2, y-1, 'H') && !has_chess(x+1, y-1)
132         || is_chess(x+2, y+1, 'H') && !has_chess(x+1, y+1)
133         || is_chess(x-1, y-2, 'H') && !has_chess(x-1, y-1)
134         || is_chess(x-1, y+2, 'H') && !has_chess(x-1, y+1)
135         || is_chess(x+1, y-2, 'H') && !has_chess(x+1, y-1)
136         || is_chess(x+1, y+2, 'H') && !has_chess(x-1, y+1));
137 }
138 
139 //判斷能否被“炮”吃掉
140 bool cannon(int x, int y)
141 {
142     int tmp = has_left(x, y, 'C');
143     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1)
144         return 1;
145     tmp = has_right(x, y, 'C');
146     if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1)
147         return 1;
148     tmp = has_up(x, y, 'C');
149     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1)
150         return 1;
151     tmp = has_down(x, y, 'C');
152     if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1)
153         return 1;
154     return 0;
155 }
156 
157 //判斷是不是死棋
158 bool is_die(int x, int y)
159 {
160     if(x < 1 || x > 3 || y < 4 || y > 6)
161         return 1;
162     return (general(x, y) || chariot(x, y) || horse(x, y) || cannon(x, y));
163 }
164 
165 int main()
166 {
167     int n, xt, yt;
168     int x, y;
169     char s[2];
170     while(scanf("%d %d %d", &n, &xt, &yt) != EOF && (n || xt || yt))
171     {
172         memset(palace, 0, sizeof(palace));
173         for(int i = 0; i < n; i++)
174         {
175             scanf("%s %d %d", s, &x, &y);
176             palace[x][y] = s[0];
177         }
178 
179         //如果“將”向周圍走一步不死或者在原地不死,就說明不是死棋
180         //(話說不知道該不該判斷原地,因為我沒讀懂題,不知道輪到誰走棋了。。。)
181         if(!is_die(xt, yt) || !is_die(xt+1, yt) || !is_die(xt-1, yt) ||
182             !is_die(xt, yt+1) || !is_die(xt, yt-1))
183             printf("NO\n");
184         else
185             printf("YES\n");
186     }
187     return 0;
188 }
View Code

?

轉載于:https://www.cnblogs.com/wolfred7464/p/3457549.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273917.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273917.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273917.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql在cmd命令行下的相關操作

1、設置新的root密碼。 mysql -u root -p 直接回車&#xff0c;無需輸入密碼就可以進入數據庫了。 此時在命令行下執行 use mysql &#xff08;切換到系統數據庫&#xff09; 執行以下語句既可修改root用戶密碼&#xff1a; update user set passwordPASSWORD("123456…

python 多繼承的問題

&#xff08;&#xff11;&#xff09;、關于Python支持多繼承&#xff0c;如果父類中有相同的方法名&#xff0c;而在子類中調用時沒有指定父類名&#xff0c;則Python解釋器將從左向右按順序進行搜索。 例如&#xff1a; class B():def a(self):print("this is B"…

python 常看

(1)、單鏈表的翻轉 參考&#xff1a; https://www.cnblogs.com/mafeng/p/7149980.html 參考代碼&#xff1a; def reverse_linkedlist2(head): if head None or head.next None: #邊界條件 return head cur head #循環變量 tmp None #保存數據的臨時變量 newhead None…

Android中的音頻播放(MediaPlayer和SoundPool)

Android中音頻和視頻的播放我們最先想到的就是MediaPlayer類了&#xff0c;該類提供了播放、暫停、停止、和重復播放等方法。該類位于android.media包下&#xff0c;詳見API文檔。其實除了這個類還有一個音樂播放類那就是SoundPool&#xff0c;這兩個類各有不同分析一下便于大家…

python中的靜態方法和類方法

一、先看語法&#xff0c;python 類語法中有三種方法&#xff0c;實例方法&#xff0c;靜態方法&#xff0c;類方法。 普通實例方法&#xff0c;第一個參數需要是self&#xff0c;它表示一個具體的實例本身。 如果用了staticmethod&#xff0c;那么就可以無視這個self&#xf…

我所遭遇過的中間件--VTK

我所遭遇過的中間件--VTK Vtk是我接觸的第一款軟件開發包,它引導我對圖形學的入門.我是先學的VTK,后學的OpenGL和D3D.VTK是專為圖形學開發,特點是接口清晰,好上手,又含有大量的圖像處理算法.從VTK入手3D圖形學,要比從OpenGL和D3D容易的多. 最初接觸VTK是研一那年暑假,研一時我做…

java.lang.OutOfMemoryError: PermGen space 問題解決

Tomcat/bin/catalina.bat 或 .sh 文件中的“rem ----- Execute The Requested Command -”這個后面增加了下面的語句set JAVA_OPTS%JAVA_OPTS% -Xms256m -Xmx1024m -XX:PermSize256M -XX:MaxNewSize256m -XX:MaxPermSize512m Myeclipse配置選項 打開選項..輸入tomcat關鍵字,然…

python中的__new__和__init__

一、__init__ 方法是什么&#xff1f; 使用Python寫過面向對象的代碼的同學&#xff0c;可能對 __init__ 方法已經非常熟悉了&#xff0c;__init__ 方法通常用在初始化一個類實例的時候。 這樣便是__init__最普通的用法了。 但__init__其實不是實例化一個類的時候第一個被調…

python 中的__getattr__和__setattr__

__getattr__為內置方法&#xff0c;當使用點號獲取實例屬性時&#xff0c;如果屬性不存在就自動調用__getattr__方法 __setattr__當設置類實例屬性時自動調用&#xff0c;如j.name5 就會調用__setattr__方法 self.[name]5 因為這個類是從dict繼承來的&#xff0c;是dict的超類 …

correct ways to define variables in python

http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say this轉載于:https://www.cnblogs.com/luomingchuan/p/3475268.html

python 的鉆石繼承問題

如果子類繼承自兩個單獨的超類&#xff0c;而那兩個超類又繼承自同一個公共基類&#xff0c;那么就構成了鉆石繼承體系。這種繼承體系很像豎立的菱形&#xff0c;也稱作菱形繼承。 class Base:def __init__(self, value):print("This is Base __init__")self.value …

認知http響應頭

HTTP&#xff08;HyperTextTransferProtocol&#xff09;是超文本傳輸協議的縮寫&#xff0c;它用于傳送WWW方式的數據&#xff0c;關于HTTP協議的詳細內 容請參考RFC2616。HTTP協議采用了請求/響應模型。客戶端向服務器發送一個請求&#xff0c;請求頭包含請求的方法、URI、協…

Python3的方法解析順序(MRO)

Python 2.3 的新式類的 C3 算法。它也是 Python 3 唯一支持的方式(筆者使用python3&#xff0c;所以就先講這種的) 一個例子&#xff1a; class D(object): pass class E(object): pass class F(object): pass class C(D, F): pass class B(E, D): …

WPF 用 DataTemplate 合并DataGrid列表列頭類似報表設計及行頭列頭樣式 - 學習

WPF中 DataGrid 列頭合并&#xff0c;類似于報表設計。效果圖如下↓ 1.新建一個WPF項目WpfApplication1&#xff0c;新建一個窗體DataGridTest&#xff0c;前臺代碼如下&#xff1a; <Window x:Class"WpfApplication1.DataGridTest" xmlns"http://sch…

python 中的pickle庫

序列化&#xff1a;我們把變量從內存中變成可存儲或傳輸的過程稱之為序列化&#xff0c;在Python中叫pickling&#xff0c;在其他語言中也被稱之為serialization&#xff0c;marshalling&#xff0c;flattening等等&#xff0c;都是一個意思。 序列化之后&#xff0c;就可以把…

他山之石,可以攻玉——來自亞馬遜的電商啟示錄

題記&#xff1a;“創新是我們的DNA&#xff0c;技術是我們改善客戶體驗的基礎2009 年致股東的信” 1. 從亞馬遜的成功講起 1.1 歷經8 年虧損始成正果 它是世界上所有電商的龍頭和楷模&#xff0c;是毫無爭議的行業標桿和旗幟&#xff0c;它在戰略和經營上的一舉一動都是關注的…

python數據結構-棧和隊列的實現

&#xff11;、棧&#xff08;后進先出(last in first out&#xff0c;LIFO)&#xff09; 棧是一種特殊的列表&#xff0c;棧內的元素只能通過列表的一端訪問&#xff0c;這一端稱為棧頂。棧被稱為一種后入先出&#xff08;LIFO&#xff0c;last-in-first-out&#xff09;的數…

c#只讀字段和常量的區別,以及靜態構造函數的使用 .

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ /// <summary> /// 功能&#xff1a;c#只讀字段和常量的區別&#xff0c;以及靜態構造函數的使用 /// </summary> class Progra…

python中的json序列化

如果我們要在不同的編程語言之間傳遞對象&#xff0c;就必須把對象序列化為標準格式&#xff0c;比如XML&#xff0c;但更好的方法是序列化為JSON&#xff0c;因為JSON表示出來就是一個字符串&#xff0c;可以被所有語言讀取&#xff0c;也可以方便地存儲到磁盤或者通過網絡傳輸…