Language: C++

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#define INF (1e9)
#define se second
#define f first
#define N 505
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
int dijkstra();
int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];

int main(){
  string s;
  cin>>w>>h>>n;
  cin>>sx>>sy>>gx>>gy;
  while(n--){
    cin>>ax>>ay;
    cin>>t>>s;
    string a=s;
    while(--t) s+=a;
    for(int i=0;i<s.size();i++){
      if(s[i]=='L'&&ax!=0) c[3][ay][ax]++,c[1][ay+1][ax]++,ax--;
      if(s[i]=='U'&&ay!=0) c[2][ay][ax]++,c[0][ay][ax+1]++,ay--;
      if(s[i]=='R'&&ax!=w-1) c[1][ay+1][ax+1]++,c[3][ay][ax+1]++,ax++;
      if(s[i]=='D'&&ay!=h-1) c[0][ay+1][ax+1]++,c[2][ay+1][ax]++,ay++;
    }
  }
  cout<<dijkstra()<<endl;
  return 0;
}

int dijkstra(){
  priority_queue<P1,vector<P1>,greater<P1> > q;
  int dy[4]={0,-1,0,1},dx[4]={-1,0,1,0};
  for(int i=0;i<=h;i++)
    for(int j=0;j<=w;j++) d[i][j]=INF;
  for(int i=0;i<4;i++) d[sy][sx]=0,q.push(P1(0,P(sy,sx)));
  while(!q.empty()){
    P1 t=q.top(); q.pop();
    int cost=t.f,y=t.se.f,x=t.se.se;
    if(d[y][x]<cost) continue;
    for(int i=0;i<4;i++){
      int ny=y+dy[i],nx=x+dx[i],co=c[i][y][x];
      if(ny<0||nx<0||h<ny||w<nx) continue;
      if(d[ny][nx]>cost+co){
	d[ny][nx]=cost+co;
	q.push(P1(cost+co,P(ny,nx)));
      }
    }
  }
  return d[gy][gx];
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#define INF (1e9)
#define se second
#define f first
#define N 505
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
int dijkstra();
int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];

int main(){
  string s;
  cin>>w>>h>>n;
  cin>>sx>>sy>>gx>>gy;
  while(n--){
    cin>>ax>>ay;
    cin>>t>>s;
    string a=s;
    while(--t) s+=a;
    for(int i=0;i<s.size();i++){
      if(s[i]=='L'&&ax!=0) c[3][ay][ax]++,c[1][ay+1][ax]++,ax--;
      if(s[i]=='U'&&ay!=0) c[2][ay][ax]++,c[0][ay][ax+1]++,ay--;
      if(s[i]=='R'&&ax!=w-1) c[1][ay+1][ax+1]++,c[3][ay][ax+1]++,ax++;
      if(s[i]=='D'&&ay!=h-1) c[0][ay+1][ax+1]++,c[2][ay+1][ax]++,ay++;
    }
  }
  cout<<dijkstra()<<endl;
  return 0;
}

int dijkstra(){
  priority_queue<P1,vector<P1>,greater<P1> > q;
  int dy[4]={0,-1,0,1},dx[4]={-1,0,1,0};
  for(int i=0;i<=h;i++)
    for(int j=0;j<=w;j++) d[i][j]=INF;
  for(int i=0;i<4;i++) d[sy][sx]=0,q.push(P1(0,P(sy,sx)));
  while(!q.empty()){
    P1 t=q.top(); q.pop();
    int cost=t.f,y=t.se.f,x=t.se.se;
    if(d[y][x]<cost) continue;
    for(int i=0;i<4;i++){
      int ny=y+dy[i],nx=x+dx[i],co=c[i][y][x];
      if(ny<0||nx<0||h<ny||w<nx) continue;
      if(d[ny][nx]>cost+co){
	d[ny][nx]=cost+co;
	q.push(P1(cost+co,P(ny,nx)));
      }
    }
  }
  return d[gy][gx];
}

Compiler output:
a.cc:70:5: error: redefinition of 'int w'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |     ^
a.cc:14:5: note: 'int w' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |     ^
a.cc:70:7: error: redefinition of 'int h'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |       ^
a.cc:14:7: note: 'int h' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |       ^
a.cc:70:9: error: redefinition of 'int n'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |         ^
a.cc:14:9: note: 'int n' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |         ^
a.cc:70:11: error: redefinition of 'int sy'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |           ^~
a.cc:14:11: note: 'int sy' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |           ^~
a.cc:70:14: error: redefinition of 'int sx'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |              ^~
a.cc:14:14: note: 'int sx' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |              ^~
a.cc:70:17: error: redefinition of 'int gy'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                 ^~
a.cc:14:17: note: 'int gy' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                 ^~
a.cc:70:20: error: redefinition of 'int gx'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                    ^~
a.cc:14:20: note: 'int gx' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                    ^~
a.cc:70:23: error: redefinition of 'int ay'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                       ^~
a.cc:14:23: note: 'int ay' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                       ^~
a.cc:70:26: error: redefinition of 'int ax'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                          ^~
a.cc:14:26: note: 'int ax' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                          ^~
a.cc:70:29: error: redefinition of 'int t'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                             ^
a.cc:14:29: note: 'int t' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                             ^
a.cc:70:31: error: redefinition of 'int c [4][505][505]'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                               ^
a.cc:14:31: note: 'int c [4][505][505]' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                               ^
a.cc:70:42: error: redefinition of 'int d [505][505]'
   70 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                                          ^
a.cc:14:42: note: 'int d [505][505]' previously declared here
   14 | int w,h,n,sy,sx,gy,gx,ay,ax,t,c[4][N][N],d[N][N];
      |                                          ^
a.cc:72:5: error: redefinition of 'int main()'
   72 | int main(){
      |     ^~~~
a.cc:16:5: note: 'int main()' previously defined here
   16 | int main(){
      |     ^~~~
a.cc:92:5: error: redefinition of 'int dijkstra()'
   92 | int dijkstra(){
      |     ^~~~~~~~
a.cc:36:5: note: 'int dijkstra()' previously defined here
   36 | int dijkstra(){
      |     ^~~~~~~~