vijos p1193 掃雷

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

 描述

相信大家都玩過掃雷的遊戲。那是在一個n*n的矩陣裡面有一些雷,要你根據一些資訊找出雷來。萬聖節到了,“餘”任過流行起了一種簡單的掃雷遊戲,這個遊戲規則和掃雷一樣,如果某個格子沒有雷,那麼它裡面的數字表示和他8連通的格子裡面雷的數目。現在棋盤是n*2的,第一列裡某些格子是雷,而第二列沒有雷,如:
o 1
* 2
* 3
* 2
o 2
* 2
* 2 (‘*‘代表有雷,‘o‘代表無雷)
由於第一類的雷有可能有多種方案滿足第二列的數的限制,你的任務即根據第二列的資訊求第一列雷有多少中擺放方案。

格式輸入格式

第一行為N,第二行有N個數,依次為第二列的格子中的數。(1<=N<=10000)

輸出格式

一個數,即第一列中雷的擺放方案數。

範例1範例輸入1[複製] 
21 1
範例輸出1[複製] 
2
限制

1s

題解:

在前兩到狀壓題的影響下,我採用了當前到第幾行,該行是否放了雷以及上一行是否放了雷,在狀態轉移的時候發現沒有一點DP的味道。。。不過還是過了。。。

其實當第一行是否放雷確定後,那麼後面的就都確定了,所以答案不會超過2,枚舉第一行的狀態即可

代碼:

1.

 1 var f:array[0..10100,0..1,0..1] of longint; 2     n,i,j,k:longint; 3     a:array[0..10100] of longint; 4 procedure init; 5  begin 6    readln(n); 7    for i:=1 to n do read(a[i]); 8  end; 9 procedure main;10  begin11    f[1,1,1]:=0;12    f[1,0,1]:=0;13    f[1,0,0]:=1;14    f[1,1,0]:=1;15    for i:=2 to n+1 do16    if a[i-1]=1 then17     begin18       f[i,1,0]:=f[i-1,0,0];19       f[i,1,1]:=0;20       f[i,0,1]:=f[i-1,1,0];21       f[i,0,0]:=f[i-1,0,1];22     end23    else24    if a[i-1]=2 then25     begin26       f[i,1,0]:=f[i-1,0,1];27       f[i,1,1]:=f[i-1,1,0];28       f[i,0,1]:=f[i-1,1,1];29       f[i,0,0]:=0;30     end31    else32    if a[i-1]=3 then33     begin34       f[i,1,0]:=0;35       f[i,1,1]:=f[i-1,1,1];36       f[i,0,1]:=0;37       f[i,0,0]:=0;38     end39    else40     begin41       f[i,1,0]:=0;42       f[i,1,1]:=0;43       f[i,0,0]:=f[i-1,0,0];44       f[i,0,1]:=0;45     end;46    writeln(f[n+1,0,1]+f[n+1,0,0]);47    end;48 begin49   assign(input,‘input.txt‘);assign(output,‘output.txt‘);50   reset(input);rewrite(output);51   init;52   main;53   close(input);close(output);54 end.      
View Code

2.

 1 var a,b:array[0..12000] of longint; 2     ans,i,n:longint; 3 function check:boolean; 4  var i,t:longint; 5    begin 6      b[2]:=a[1]-b[1]; 7      for i:=2 to n do 8       begin 9         t:=b[i]+b[i-1];10         if (t>a[i]) or (t+1<a[i]) then exit(false);11         b[i+1]:=a[i]-t;12       end;13      exit(b[i+1]<>1);14    end;15 16 begin17   assign(input,‘input.txt‘);assign(output,‘output.txt‘);18   reset(input);rewrite(output);19   readln(n);20   for i:=1 to n do read(a[i]);21   ans:=0;22   for i:=0 to 1 do23    begin24      b[1]:=i;25      if check then inc(ans);26    end;27   writeln(ans);28   close(input);close(output);29 end.    
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.