标签:
2 0 0 5 5 2 3 3 5 2 5 5 3 3 1 4 4
11 0
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 const int maxn = 500; 5 const int dir[4][2] = {-1,0,0,1,0,-1,1,0}; 6 #define MAXN 200 7 struct HP { 8 int len,s[MAXN]; 9 HP() { 10 memset(s,0,sizeof(s)); 11 len=1; 12 } 13 HP operator =(const char *num) { //字符串赋值 14 len=strlen(num); 15 for(int i=0; i<len; i++) s[i]=num[len-i-1]-‘0‘; 16 } 17 18 HP operator =(int num) { //int 赋值 19 char s[MAXN]; 20 sprintf(s,"%d",num); 21 *this=s; 22 return *this; 23 } 24 25 HP(int num) { 26 *this=num; 27 } 28 29 HP(const char*num) { 30 *this=num; 31 } 32 33 string str()const { //转化成string 34 string res=""; 35 for(int i=0; i<len; i++) res=(char)(s[i]+‘0‘)+res; 36 if(res=="") res="0"; 37 return res; 38 } 39 40 HP operator +(const HP& b) const { 41 HP c; 42 c.len=0; 43 for(int i=0,g=0; g||i<max(len,b.len); i++) { 44 int x=g; 45 if(i<len) x+=s[i]; 46 if(i<b.len) x+=b.s[i]; 47 c.s[c.len++]=x%10; 48 g=x/10; 49 } 50 return c; 51 } 52 void clean() { 53 while(len > 1 && !s[len-1]) len--; 54 } 55 56 HP operator *(const HP& b) { 57 HP c; 58 c.len=len+b.len; 59 for(int i=0; i<len; i++) 60 for(int j=0; j<b.len; j++) 61 c.s[i+j]+=s[i]*b.s[j]; 62 for(int i=0; i<c.len-1; i++) { 63 c.s[i+1]+=c.s[i]/10; 64 c.s[i]%=10; 65 } 66 c.clean(); 67 return c; 68 } 69 70 HP operator - (const HP& b) { 71 HP c; 72 c.len = 0; 73 for(int i=0,g=0; i<len; i++) { 74 int x=s[i]-g; 75 if(i<b.len) x-=b.s[i]; 76 if(x>=0) g=0; 77 else { 78 g=1; 79 x+=10; 80 } 81 c.s[c.len++]=x; 82 } 83 c.clean(); 84 return c; 85 } 86 HP operator / (const HP &b) { 87 HP c, f = 0; 88 for(int i = len-1; i >= 0; i--) { 89 f = f*10; 90 f.s[0] = s[i]; 91 while(f>=b) { 92 f =f-b; 93 c.s[i]++; 94 } 95 } 96 c.len = len; 97 c.clean(); 98 return c; 99 } 100 HP operator % (const HP &b) { 101 HP r = *this / b; 102 r = *this - r*b; 103 return r; 104 } 105 106 HP operator /= (const HP &b) { 107 *this = *this / b; 108 return *this; 109 } 110 111 112 HP operator %= (const HP &b) { 113 *this = *this % b; 114 return *this; 115 } 116 117 bool operator < (const HP& b) const { 118 if(len != b.len) return len < b.len; 119 for(int i = len-1; i >= 0; i--) 120 if(s[i] != b.s[i]) return s[i] < b.s[i]; 121 return false; 122 } 123 124 bool operator > (const HP& b) const { 125 return b < *this; 126 } 127 128 bool operator <= (const HP& b) { 129 return !(b < *this); 130 } 131 132 bool operator == (const HP& b) { 133 return !(b < *this) && !(*this < b); 134 } 135 bool operator != (const HP &b) { 136 return !(*this == b); 137 } 138 HP operator += (const HP& b) { 139 *this = *this + b; 140 return *this; 141 } 142 bool operator >= (const HP &b) { 143 return *this > b || *this == b; 144 } 145 146 147 }; 148 149 istream& operator >>(istream &in, HP& x) { 150 string s; 151 in >> s; 152 x = s.c_str(); 153 return in; 154 } 155 156 ostream& operator <<(ostream &out, const HP& x) { 157 out << x.str(); 158 return out; 159 } 160 int dp[maxn][maxn],sx,sy,m,n,p; 161 HP cnt[maxn][maxn]; 162 bool isIn(int x,int y) { 163 return x >= 0 && y >= 0 && x < 201 && y < 201; 164 } 165 struct node { 166 int x,y; 167 node(int x = 0,int y = 0) { 168 this->x = x; 169 this->y = y; 170 } 171 }; 172 queue<node>q; 173 void bfs() { 174 while(!q.empty()) q.pop(); 175 if(dp[sx][sy] == -1) { 176 dp[sx][sy] = 0; 177 cnt[sx][sy] = 1; 178 q.push(node(sx,sy)); 179 } 180 while(!q.empty()) { 181 node now = q.front(); 182 q.pop(); 183 if(now.x == m && now.y == n && dp[now.x][now.y] != abs(m - sx) + abs(n - sy)) break; 184 for(int i = 0; i < 4; ++i) { 185 int nx = now.x + dir[i][0]; 186 int ny = now.y + dir[i][1]; 187 if(!isIn(nx,ny) || dp[nx][ny] == INF) continue; 188 if(dp[nx][ny] == -1 || dp[nx][ny] > dp[now.x][now.y] + 1) { 189 dp[nx][ny] = dp[now.x][now.y] + 1; 190 cnt[nx][ny] = cnt[now.x][now.y]; 191 q.push(node(nx,ny)); 192 } else if(dp[nx][ny] == dp[now.x][now.y] + 1) { 193 cnt[nx][ny] += cnt[now.x][now.y]; 194 } 195 } 196 } 197 } 198 int main() { 199 int x,y,kase; 200 scanf("%d",&kase); 201 while(kase--) { 202 scanf("%d%d%d%d",&sx,&sy,&m,&n); 203 scanf("%d",&p); 204 memset(dp,-1,sizeof dp); 205 memset(cnt,0,sizeof cnt); 206 while(p--) { 207 scanf("%d%d",&x,&y); 208 dp[x][y] = INF; 209 for(int i = 0; i < 4; ++i) { 210 int nx = x + dir[i][0]; 211 int ny = y + dir[i][1]; 212 if(!isIn(nx,ny)) continue; 213 dp[nx][ny] = INF; 214 } 215 } 216 bfs(); 217 if(dp[m][n] != abs(m - sx) + abs(n - sy)) cnt[m][n] = 0; 218 cout<<cnt[m][n]<<endl; 219 } 220 return 0; 221 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4800207.html