Movatterモバイル変換


[0]ホーム

URL:


はてラボはてな匿名ダイアリー
ようこそ ゲスト さんログインユーザー登録
< http://anond.hatelabo.jp/20110... |自分の息子を殺した事... >

2011-03-03

ブログの問題を解いてみた

http://okajima.air-nifty.com/b/2011/01/2011-ffac.html

ぷよぷよを解く問題をやってみた

かかった時間はおおよそ1時間

途中でわからないところがあったのでくぐってしまった

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespaceConsoleApplication1{ class Program    {        staticvoid Main(string[] args)        {StringBuilder[]blocks = {                newStringBuilder("**GYRR"),                newStringBuilder("RYYGYG"),                newStringBuilder("GYGYRR"),                newStringBuilder("RYGYRG"),                newStringBuilder("YGYRYG"),                newStringBuilder("GYRYRG"),                newStringBuilder("YGYRYR"),                newStringBuilder("YGYRYR"),                newStringBuilder("YRRGRG"),                newStringBuilder("RYGYGG"),                newStringBuilder("GRYGYR"),                newStringBuilder("GRYGYR"),                newStringBuilder("GRYGYR")            };            bool updated = true;            while (updated)            {            breaked:                DumpBlock(blocks);                for (int i = 0; i &lt;blocks.Length; i++)                {                    for (int j = 0; j &lt;blocks[i].Length; j++)                    {char c =blocks[i][j];                        if (c == '*')continue;                        updated = false;                        if (KillBlocks(blocks, i, j))                        {                            updated = true;goto breaked;                        }                    }                }            }            DumpBlock(blocks);Console.Read();        }        structPoint        {            public int x, y;            publicPoint(int x, int y)            {                this.x = x;                this.y = y;            }        }        static bool KillBlocks(StringBuilder[]blocks, int x, int y)        {            bool[,] visted = new bool[blocks.Length,blocks[0].Length];            MarkBlock(visted,blocks, x, y);            Queue&lt;Point&gt; queque = new Queue&lt;Point&gt;();            for (int i = x; i &lt;blocks.Length; i++)                for (int j = y; j &lt;blocks[i].Length; j++)                    if(visted[i,j] == true)                        queque.Enqueue(newPoint(j,i));            if (queque.Count &lt; 4)                return false;            while (queque.Count &gt; 0)            {Point p = queque.Dequeue();                RemoveBlock(blocks, p.x, p.y);            }            return true;        }        staticvoid MarkBlock(bool[,] visted,StringBuilder[]blocks, int x, int y)        {            if (x &lt; 0 || y &lt; 0 || x &gt;=blocks.Length || y &gt;=blocks[0].Length || visted[x, y] == true)                return;char c =blocks[x][y];            visted[x, y] = true;            if (x + 1 &lt;blocks.Length &amp;&amp;blocks[x + 1][y] == c) MarkBlock(visted,blocks, x + 1, y);            if (y + 1 &lt;blocks[0].Length &amp;&amp;blocks[x][y + 1] == c) MarkBlock(visted,blocks, x, y + 1);            if (x &gt; 0 &amp;&amp;blocks[x - 1][y] == c) MarkBlock(visted,blocks, x - 1, y);            if (y &gt; 0 &amp;&amp;blocks[x][y - 1] == c) MarkBlock(visted,blocks, x, y - 1);        }        staticvoid DumpBlock(StringBuilder[]blocks)        {            foreach (StringBuilder s inblocks)Console.WriteLine(s);Console.WriteLine();        }        staticvoid RemoveBlock(StringBuilder[]blocks,int x,int y)        {            int i;            if (y == 0)            {blocks[y][x] = '*';                return;            }            for (i = y; i &gt; 0; i--)            {blocks[i][x] =blocks[i - 1][x];            }blocks[i][x] = '*';        }    }}

Permalink |記事への反応(1) | 22:53

このエントリーをはてなブックマークに追加ツイートシェア

記事への反応 -
  • じゃぁ、問1をやってみた。 unsigned int f(unsigned int x) { x = x - 1; x = x | (x >> 1); x = x | (x >> 2); x = x | (x >> 4); x = x | (x >> 8); x = x | (x >>16); return x + 1;}int main(int argc, _TC...

記事への反応(ブックマークコメント)

全てのコメントを見る

人気エントリ

注目エントリ

ログインユーザー登録
ようこそ ゲスト さん
Copyright (C) 2001-2025 hatena. All Rights Reserved.

[8]ページ先頭

©2009-2025 Movatter.jp