Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int isNearby(int distance, char element, char map[10][10], int height, int width, int y, int x) {
  4. --x;
  5. --y;
  6. if(distance == 0 && map[y][x] == element) {
  7. return 1;
  8. }
  9. int i;
  10. for(i = y - distance; i < y + distance; ++i) {
  11. if(i >= height || i < 0) {
  12. continue;
  13. }
  14. int j;
  15. for(j = x - distance; j < x + distance; ++j) {
  16. if(j >= width || j < 0) {
  17. continue;
  18. }
  19. if(map[i][j] == element) {
  20. return 1;
  21. }
  22. }
  23. }
  24. return 0;
  25. }
  26.  
  27. int main(){
  28.  
  29. int height, width, waterDistance, mountainDistance, sandDistance;
  30. int i, j;
  31.  
  32. char water = '~';
  33. char mountain = 'A';
  34. char sand = 'X';
  35.  
  36. FILE* input=fopen("be.txt", "r");
  37.  
  38. fscanf(input, "%d %d %d", &waterDistance, &mountainDistance, &sandDistance);
  39. fscanf(input, "%d %d\n", &width, &height);
  40. printf("Maximal water distance: %d\nMaximal mountain distance: %d\nMinimal sand distance: %d\n", waterDistance, mountainDistance, sandDistance);
  41. char map[height][width];
  42. char line[20000];
  43. for (i=0;i<height;i++){
  44. fgets(line,200000,input);
  45. for(j=0;j<width;j++){
  46. map[i][j]=line[j];
  47. printf("%c",map[i][j]);
  48. }
  49. printf("\n");
  50. }
  51. fclose(input);
  52. int y, x;
  53. int count = 0;
  54. for(x = 1; x <= width; ++x) {
  55. for(y = 1; y <= height; ++y) {
  56. if(map[y][x] == mountain) {
  57. continue;
  58. }
  59. if(isNearby(waterDistance, water, map, height, width, y, x) == 1
  60. && isNearby(mountainDistance, mountain, map, height, width, y, x) == 1
  61. && isNearby(sandDistance, sand, map, height, width, y, x) != 1) {
  62. printf("%d|%d ---> %hc\n", x, y, map[y][x]);
  63. ++count;
  64. }
  65. }
  66. }
  67. printf("\n%d\n", count);
  68.  
  69.  
  70. //printf("%d\n", hely);
  71. /*
  72. FILE* output=fopen("ki.txt", "w");
  73. fprintf(output, "%d\n", hely);
  74. fclose(output);
  75. */
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement