第10回日本情報オリンピック 予選(オンライン)の過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
「競技プログラミング」を略して、「競プロ」などと呼ばれています。
#E - チーズ (Cheese)
atcoder.jp
僕が作成、提出したコードは、以下のとおりです。
/* AtCoder Problems in C++ #E - チーズ (Cheese) https://atcoder.jp/contests/joi2011yo/tasks/joi2011yo_e 提出コードの解答例 https://neguse-atama.hatenablog.com */ #include<bits/stdc++.h> using namespace std; int H,W,N; char s[1001][1001]; int dp[1001][1001][11]; bool tf[1001][1001][11]; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; void BFS(int sx,int sy,int n){ tf[sx][sy][n]=true; queue<pair<pair<int,int>,int>> que; que.push({{sx,sy},n}); dp[sx][sy][n]=0; while(!que.empty()){ pair<pair<int,int>,int> p; p=que.front(); que.pop(); int x=p.first.first; int y=p.first.second; int nn=p.second; for(int i=0;i<4;i++){ int nx=x+dx[i]; int ny=y+dy[i]; if(nx<0 || ny<0 || nx>=H || ny>=W || tf[nx][ny][nn] || s[nx][ny]=='X' || nn>N){ continue; } tf[nx][ny][nn]=true; if(s[nx][ny]-'0'==nn){ dp[nx][ny][nn+1]=dp[x][y][nn]+1; que.push({{nx,ny},nn+1}); }else{ dp[nx][ny][nn]=dp[x][y][nn]+1; que.push({{nx,ny},nn}); } } } } int main(void){ cin>>H>>W>>N; int sx,sy,gx,gy; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin>>s[i][j]; if(s[i][j]=='S') { sx=i,sy=j; } if(s[i][j]-'0'==N){ gx=i,gy=j; } } } memset(tf,0,sizeof(tf)); BFS(sx,sy,1); cout<<dp[gx][gy][N+1]<<endl; return 0; }
第10回日本情報オリンピック 予選(オンライン)は、オンラインジャッジによるプログラミングコンテストです。
AtCoderのコンテストは日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。