{"id":657,"date":"2017-06-05T10:45:04","date_gmt":"2017-06-05T13:45:04","guid":{"rendered":"http:\/\/www.galirows.com.br\/meublog\/programacao\/?p=657"},"modified":"2021-06-18T15:10:02","modified_gmt":"2021-06-18T18:10:02","slug":"mapa-do-tesouro","status":"publish","type":"post","link":"http:\/\/www.galirows.com.br\/meublog\/programacao\/mapa-do-tesouro\/","title":{"rendered":"Mapa do tesouro"},"content":{"rendered":"\n<p>Nesse post proponho um algoritmo para solucionar&nbsp;uma ca\u00e7a ao tesouro a partir de um mapa. O algoritmo inicia solicitando o mapa e depois avalia se, a partir do mapa especificado, \u00e9 poss\u00edvel alcan\u00e7ar o tesouro seguindo as instru\u00e7\u00f5es.<\/p>\n\n\n\n<p>O mapa aceita os caracteres &#8220;&gt;&#8221;, &#8220;v&#8221;, &#8220;&lt;&#8220;, &#8220;^&#8221;, &#8220;*&#8221; e &#8220;.&#8221;, indicando respectivamente: &#8220;ir para a direita&#8221;, &#8220;ir para baixo&#8221;, &#8220;ir para a esquerda&#8221;, &#8220;ir para cima&#8221;, &#8220;lugar do tesouro&#8221; e &#8220;caminho sem nada&#8221;.&nbsp;O mapa sempre come\u00e7a na posi\u00e7\u00e3o 0,0 da matriz.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Solu\u00e7\u00e3o em C\/C++<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code lang:c decode:true\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n \nint main(void) {\n\tint lin, col;\n\tscanf(\"%i %i\\n\", &amp;lin, &amp;col);\n\tint mapa[lin][col];\n\tint i, j;\n \n\t\/\/preenche matriz\n\tfor (i = 0; i &lt; lin; i++) {\n\t\t\/\/fflush(stdin);\n\t\tfor (j = 0; j &lt; col; j++) {\n    \t\t\tscanf(\"%c\", &amp;mapa[i][j]);\n\t\t}\n\t}\n \n\tfor (i = 0; i &lt; lin; i++) {\n\t    \tfor (j = 0; j &lt; col; j++) {\n    \t\t\tprintf(\"%c\", mapa[i][j]);\n\t    \t}\t\n    \t\tprintf(\"\\n\");\n\t}\n \n\t\/\/percorre o mapa\n\tint posX = 0, posY = 0;\n\tchar direcao = '&gt;', leitura;\n \n\twhile (posX &lt; 10) {\n\t    leitura = mapa[posY][posX];\n\t    if (leitura == '&gt;') {\n\t        direcao = '&gt;';\n\t        posX = posX + 1;\n\t    } else if (leitura == '&lt;') {\n\t        direcao = '&lt;';\n\t        posX = posX - 1;\n\t    } else if (leitura == '^') {\n\t        direcao = '^';\n\t        posY = posY - 1;\n\t    } else if (leitura == 'v') {\n\t        direcao = 'v';\n\t        posY = posY + 1;\n\t    } else if (leitura == '*') {\n\t        printf(\"Achou o tesouro!\");\n\t        break;\n\t    } else { \/\/nao ouve mudanca de direcao\n\t        if (direcao == '&gt;') { posX = posX + 1; }\n\t        if (direcao == '&lt;') { posX = posX - 1; }\n\t        if (direcao == '^') { posY = posY - 1; }\n\t        if (direcao == 'v') { posY = posY + 1; }\n\t    }\n \n\t    printf(\"X e Y: %i %i |%c|%c|\\n\", posX, posY, leitura, direcao);\n\t}\n \n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Teste o c\u00f3digo:<\/strong>&nbsp;<a href=\"http:\/\/ideone.com\/FBj1Ac\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/ideone.com\/FBj1Ac<\/a><\/p>\n\n\n\n<p><strong>Solu\u00e7\u00e3o em Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code lang:python decode:true\"><code lang=\"python\" class=\"language-python\">tamX = 3\ntamY = 6\nmapa = [['&gt;','.','.','v','.','.'], \n        ['.','.','.','.','.','.'],\n        ['.','.','.','&gt;','.','*']]\n\nfor i in range(tamX):\n    for j in range(tamY):\n        print mapa[i][j],\n    print\n\nposX = 0\nposY = 0\ndirecao = '&gt;'\nwhile (True):\n    if mapa[posY][posX] == '&gt;':\n        direcao = '&gt;'\n        posX = posX + 1\n    elif mapa[posY][posX] == '&lt;':\n        direcao = '&lt;'\n        posX = posX - 1\n    elif mapa[posY][posX] == '^':\n        direcao = '^'\n        posY = posY - 1\n    elif mapa[posY][posX] == 'v':\n        direcao = 'v'\n        posY = posY + 1\n    elif mapa[posY][posX] == '*':\n        print \"Achou o tesouro!\"\n        break\n    else:\n        if direcao == '&gt;':\n            posX = posX + 1\n        if direcao == '&lt;':\n            posX = posX - 1\n        if direcao == '^':\n            posY = posY + 1\n        if direcao == 'v':\n            posY = posY + 1\n              \n    print 'X e Y: ', posX, ' ', posY<\/code><\/pre>\n\n\n\n<p><strong>Teste o c\u00f3digo:<\/strong>&nbsp;<a href=\"http:\/\/www.codeskulptor.org\/#user43_PXFlhtIcec_1.py\">http:\/\/www.codeskulptor.org\/#user42_PXFlhtIcec_0.py<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nesse post proponho um algoritmo para solucionar&nbsp;uma ca\u00e7a ao tesouro a partir de um mapa. O algoritmo inicia solicitando o mapa e depois avalia se, a partir do mapa especificado, \u00e9 poss\u00edvel alcan\u00e7ar o tesouro seguindo as instru\u00e7\u00f5es. O mapa aceita os caracteres &#8220;&gt;&#8221;, &#8220;v&#8221;, &#8220;&lt;&#8220;, &#8220;^&#8221;, &#8220;*&#8221; e &#8220;.&#8221;, indicando respectivamente: &#8220;ir para a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,5],"tags":[60,36,37,50,13],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-c","category-python","tag-exercicio-resolvido","tag-for","tag-if","tag-ludico","tag-matriz"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/comments?post=657"}],"version-history":[{"count":3,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":1053,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/657\/revisions\/1053"}],"wp:attachment":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/media?parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/categories?post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/tags?post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}